最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to show posts from multiple post types in a single loop? And display them separately on the same template

programmeradmin0浏览0评论

I have four post types

  1. Products
  2. Banners
  3. Portfolio
  4. Testimonials

And they all display their posts on the same home page template (index.php).

Currently using below query to get posts from different post types.

<?php
query_posts(
    array(
        'post_type' => 'work_projects',
        'work_type' => 'website_development',
        'posts_per_page' => 100
    )
);
if ( have_posts() ) : while ( have_posts() ) : the_post();

    the_post_thumbnail($size);
    the_title();
    the_content();

endwhile; endif;

But my problem is I have to put multiple query_post loops on the same page. I googled some solutions and found answers as well here on Stack Overflow also. But not able to figure it out how to use them with the_title(), the_content() and the_post_thumbnail().

I have four post types

  1. Products
  2. Banners
  3. Portfolio
  4. Testimonials

And they all display their posts on the same home page template (index.php).

Currently using below query to get posts from different post types.

<?php
query_posts(
    array(
        'post_type' => 'work_projects',
        'work_type' => 'website_development',
        'posts_per_page' => 100
    )
);
if ( have_posts() ) : while ( have_posts() ) : the_post();

    the_post_thumbnail($size);
    the_title();
    the_content();

endwhile; endif;

But my problem is I have to put multiple query_post loops on the same page. I googled some solutions and found answers as well here on Stack Overflow also. But not able to figure it out how to use them with the_title(), the_content() and the_post_thumbnail().

Share Improve this question edited Feb 14, 2017 at 14:26 Max Yudin 6,3882 gold badges26 silver badges36 bronze badges asked Feb 14, 2017 at 14:02 Soeb SafiSoeb Safi 4933 gold badges6 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

post_type accepts an array. Have you tried using WP_Query with an array?

$args = array( 
         'post_type' => array( 'product', 'banner', 'portfolio', 'testimonial'),
         'post_status' => 'publish',
         'posts_per_page' => 100
    );
    $the_query = new WP_Query( $args );

Best practice would be to use four different custom queries and loop through each one separately using wp_query

So here's an example:

<?php
$custom_query = new WP_Query( 
    array(
        'post_type' => 'work_projects', 
        'work_type' => 'website_development', 
        'posts_per_page' => 100
    ) 
);
if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();

    the_post_thumbnail($size);
    the_title();
    the_content();

endwhile; endif; wp_reset_query();
?>

And then you would repeat the process for each post type, replacing 'work_projects' with the name of the next post type.

I'm not sure what 'work_type' is in this scenario, but I just put it back into the query since you already had it there.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论