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

I have four post typesProductsBannersPortfolioTestimonialsAnd they all display their posts on the same home page temp

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744788217a4593756.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信