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;
Table of Contents
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.
- Firstly, set the argument to get the custom post type data.
- So then, add wp_query to display all post data as you like to show on your page.
- After that, implement this code to custom post type in a loop.
- 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](https://wlearnsmart.com/wp-content/uploads/2020/07/display-custom-post-type-in-wordpress.png)
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.
- the_content() this is the content of the showing post. Also might be the passed parameter.
- the_title() here you display post title with using this title function.
- the_post_thumbnail() for this reference to get post image and pass a parameter for thumbnail size.
- get_site_url() use this function to get mail site URL link.
- get_post_permalink() So this is to get post URL link.
- get_the_ID() And then you can display post id and apply.
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](https://wlearnsmart.com/wp-content/uploads/2020/07/how-to-display-custom-post-type-by-category.png)
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.
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!
sure
Thank you for sharing your thoughts. I truly appreciate your efforts and I will be waiting for your next post thank you once again.
sure sir thanks for coming
I really like what you guys are up too. This kind of clever work and reporting!
Keep up the terrific works guys I’ve added you guys to blogroll.
Good respond in return of this query with genuine arguments and telling the whole thing on the topic of
that.
What other place may just I am getting that sort of data written in a particularly ideal strategy?