How to Use WP_Query to Display Custom Post Type in WordPress

Here, I am going to explain the use of wp_query to display custom post type in WordPress. Also, given the best method to apply for get custom post type data using WordPress.

As well as, more discuss code with example about wp_query custom post type category and taxonomy. Now, displaying multiple ways to conditions like create custom post type in the loop.

$args = array(  
        'post_status' => 'publish',
        'posts_per_page' => 5,  
        
		);
	
    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
        the_title(); 
	the_excerpt();
    endwhile;

How to Get/Display Custom Post Type in WordPress

Hence, as you know I define an example above use to wp-query for display custom post. So, there are many conditions on this code.

Here, when you use wp-query to get the post to apply these steps one by one.

  1. Firstly, set the argument to get the custom post type data.
  2. So then, add wp_query to display all post data as you like to show on your page.
  3. After that, implement this code to custom post type in a loop.
  4. Similarly, got the post data and apply the_title(), the_excerpt() and the_thumbnail() in wordpress.
$args = array(         
        'post_status' => 'publish',
        'posts_per_page' => 2,  
        
		);
	
    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
		?>
		<h2>Display Custom Post Data</h2>
		<h3><?php the_title(); ?></h3> 
		</p><?php the_excerpt(); ?></p>
		<?php
	endwhile;

Under, this code starting define a variable $args. Accordingly, this argument passes multiple values for display custom post by id and category so on.

Now, we only pass two-parameter. Only those posts visible which we publish this is the condition in $args.

Note: You can pass args value as you want to display posts according to you. There are multiple args values have provided in the below section find it.

Afterward, added wp_query and pass an argument with this get post query function. So then, make an object for the wp-query name is $loop.

Here is the output of display custom post type example. there is only showing post title and post description.

display custom post type in wordpress

WordPress Display Custom Post Type in Loop

As a rule, we created while loop for display post data using the loop.

So, you can see the above code section with it. As well as, WordPress provides some function to apply for this condition to pass with args.

while ( $loop->have_posts() ) : $loop->the_post(); 
		?>
		<h2>Display Custom Post Data</h2>
		<h3><?php the_title(); ?></h3> 
		</p><?php the_excerpt(); ?></p>
		<?php
	endwhile;

In this loop, as you want to display data on your page and conditions in any area. Here, we have to show you some examples to apply to them in your custom post loop.

Similarly, I have to provide some important parameter properties to pass the args variable for display custom post according to this.

Firstly, show the author parameter. using this parameter you can get a post with author name and id.

As well as, add some id in this function to avoid the author’s post. This is the parameter. Also, we define some important basic post meta and post meta by key with the example.

$args = array(     
         'author_name' => 'sohan',
         'author' => 555,
         'cat' => 1,
         'tag' => 'house'
         'tag_id' => 55
         'category_name' => 'mobile'   
         'post_status' => 'publish',
         'posts_per_page' => 2,  
        
	);

wp_query Custom Post Type Category

Thus, you can display custom post type data by category. Therefore, you might add some parameter value in the args variable.

So, when you mention the category key in array. After that, show only specific a post through category.

Now, I have created a custom post type example in the below code with the category meta key using WordPress.

$args = array(         
        'post_status' => 'publish',
        'posts_per_page' => 2,
		'category_name' => 'Shirt'	
        
		);
	
    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
			
		the_title();
		the_excerpt();
	
	endwhile;

Hence, you can use this code to display the custom post type by the display. Therefore, we added the category name key meta in the args array.

So, you can apply this code and change as per you want to show products and posts by category.

how to display custom post type by category

As an example, you can learn more about important terms.

How to Create Custom Post Type in Front End

Hence, if you want to display custom post type data front end. Also, you can display the homepage and any other page.

Therefore, there are many ways to show post data in the front end. Firstly, you can create a template to get the post data and choose this template any page.

If you want to learn WordPress Custom Page Template check it and apply it in the WordPress page.

<?php
/**
 * Template Name: Custom page 
 */ 
		
		$args = array(         
        'post_status' => 'publish',
        'posts_per_page' => 10,			
        
		);
	
    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
			
		the_title();
		the_excerpt();
	
	endwhile;

    wp_reset_postdata(); 

Here, we implement this PHP code standard of WordPress to create a template for display custom posts in the front end.

Secondly, you can add this template get_template_part( ‘templates/post data’ ). any other page adds this code to display post front end and homepage any other page with it.

Again, one other way to display custom post data in WordPress. Under, any page like homepage inside directly put this wp-query with your design structure. As well as, you want to show on post.

WordPress Create Custom Post by Taxonomy

This is a taxonomy to specific filter data for display post. Also, there are created a meta key values and in array to taxonomy, the array gets the filter specific data.

Mostly, use this taxonomy for search data from the post. Now, I have to show you an example of taxonomy in the below code.

		$args = array(         
        'post_status' => 'publish',
        'posts_per_page' => 10,
		'taxonomy' => 'post_tag',	
        
		);
	
    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
			
		the_title();
		the_excerpt();
	
	endwhile;

Conclusion

Finally, you can see in detail use wp_query to display custom post type in WordPress. So, if you have regarding the issue can ask any time in the comment section and apply this example on your page. Actually, in this article have some example of code.

2 Comments

  1. Hi there! Do you know if they make any plugins to help with SEO?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.
    If you know of any please share. Many thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

WORDPRESS

Recent Posts

Related Post

How to Change Logo Size in WordPress

Here, I am going to start in detail that how to change logo size in WordPress Using HTML and CSS code. Also, explain the resized logo in any WordPress theme. Usually, we are providing easily method to change logo size in Html. As well as, given some examples of live WordPress themes. So, follow some […]

Read more

How to Change Link Color in WordPress & Hover Posts/Pages

Here, we provide a code to change link color in WordPress. Also, change the hover color of the page and post using CSS. As well as, give you the best way to change hyperlink color individual page or post. Hence, I explain to the link changing color without code. Therefore, you added a plugin and […]

Read more

How to Hide Featured Image in WordPress Post & Page

Let’s see, How you can simply hide featured image in WordPress. As well as, you can remove featured image individual posts and pages with the example of code. Here, we discuss many methods for hiding the feature images in WordPress. According to, Featured images are added to different WordPress themes in their own way. Likewise, […]

Read more

x