Using WP_Query with Custom Post Types

Using WP_Query with Custom Post Types

WordPress is a powerful blog management platform that allows you to display your posts in an organized manner.

But, you might be wondering, “How can I show my custom post type on the home page?”

There are many different ways, but one of our favorites is with WP_Query.

This tutorial will walk through how to use it and why it’s such an excellent tool for your website.

WP_QUERY queries are some of the most valuable tools available to web designers when building their site because they let us find anything we might want without having to write code all over our pages or templates from scratch every time–but don’t worry if this sounds complicated; we’re going break down each step like so read on!

  1. WP_Query can be used to limit a search by post type. Use the ‘posttype’ parameter, set it as your custom post types slug, and you’ll only get results from that specific type of content.
  2. You can set the ‘post_status’ in WordPress to published or draft. Published posts will be live on your website, while drafts are published only for editing purposes.
  3. You may choose to fetch the number of posts you’d like on a page using the parameter ‘posts_per_page.’
  4. With the WP_Query class, you can order posts by title or ascending. Ordering is done with an ‘orderby’ and ‘order’ parameter, respectively. Complete your query setup using those two parameters before passing them into a WordPress while loop to display post titles and excerpts ordered according to these two variables.

Are you finding it tedious to learn all about WordPress WP_Query?

Top Article:  Are WordPress Hosts Faster Than Shared Web Hosting?

Then, we have an answer for you.

Our team of expert web developers will help you through the entire process. 

WP_Query Custom Post Type Examples

The First Example

/**
 * Setup query to show the ‘services’ post type with ‘8’ posts.
 * Output the title with an excerpt.
 */
    $args = array(  
        'post_type' => 'services',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby’ => 'title', 
        'order’ => 'ASC', 
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
        print the_title(); 
        the_excerpt(); 
    endwhile;

    wp_reset_postdata(); 

The Output

Suppose this query is run on the WordPress template, you will get an output similar to the following:

Article Title
Example excerpt of the article.. Read More

Article Title

Example excerpt of the article.. Read More

A More Advanced Example

You can now filter posts by category and get the featured image! In this example, we use WP_Query to find only those blog posts with a ‘home’ in their category.

Then using our loop, you will see how we display them with title & excerpt and the post’s featured image.

/**
 * Setup query to show the ‘services’ post type with all posts filtered by 'home' category.
 * Output is linked title with featured image and excerpt.
 */
   
    $args = array(  
        'post_type' => 'services',
        'post_status' => 'publish',
        'posts_per_page' => -1, 
        'orderby' => 'title', 
        'order' => 'ASC',
        'cat' => 'home',
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
        $featured_img = wp_get_attachment_image_src( $post->ID );
        print the_title();
        if ( $feature_img ) {
           < img src="print $featured_img['url']" width=”print $featured_img['width']" height="print $featured_img['height']" />
        }
        the_excerpt(); 
    endwhile;

    wp_reset_postdata(); 

Parameters

You can utilize several parameters to customize the posts requested with WP_Query. Below are some frequently used parameters.

  • cat – posts are filtered using a particular id
  • tag – posts are filtered using a particular tag slug
  • tax_query – posts are filtered using certain taxonomy parameters
  • s – posts are filtered using a particular search keyword
  • Author – Posts are filtered using keying in a particular author
Top Article:  10 Best WordPress LMS Plugins

Template Tags

While creating your post-type loop, there are many template tags available for use inside the loop to get your desired output. Below are various template tag samples available for use inside your loop:

The WP_Query API is an invaluable tool for developers, but it can be hard to get started.

This article will walk you through the basics of customization with a WordPress custom post type so that when you need any information about your visitors, all you will have to do is click one button and find everything in seconds.

Charlie has been building WordPress themes, reviewing web hosts and utilizing social media since their respective inceptions.

Leave a Reply