Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WordPress, do I need multiple WP_Query objects to display multiple category AND custom type post in the front page?

Tags:

php

wordpress

First thing, English is not my native language, and I am pretty new to programming, PHP and WordPress, so please be patient and very simple in your answers :)

I am creating a custom theme for a multi-feature blog displaying videos, articles, movie reviews, image galleries, events and a shopping area where visitors can buy and download some e-books.

In the front page I want to show a sort of general summary of last entries, including:

  • last 3 entries for each of 3 different blog-post categories (articles, movie reviews, videos);
  • next 3 incoming events
  • last 3 past events
  • last 3 published galleries
  • last 3 downloadable e-books

My question is: do I need to call 7 different WP_Query objects to have it working, one for each category/post_type, or are there better ways to get it done?

Maybe using the pre_get_posts hook, or some conditional tags like get_post_type() or is_category()? I tried those and some other ways but my lack of programming skills got me nowhere. Any suggestion is very welcome.

(added the relevant parts of code I am actually using)

<!-- // start Front Page contents - BLOG-->
    <!-- start tab dei post -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'post' ) ); ?>
            <!--// seleziona risultati per categoria - POST -->
            // here is included the Loop for - POST
        <?php wp_reset_postdata(); ?>
    <!-- end tab dei post -->
    <!-- start tab rece -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'rece' ) ); ?>
            <!--// seleziona risultati per categoria - RECE -->
            // here is included the Loop for - REVIEWS
        <?php wp_reset_postdata(); ?>
    <!-- end tab rece -->
    <!-- start tab video -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'vide' ) ); ?>
            <!--// seleziona risultati per categoria - VIDE -->
            // here is included the Loop for - VIDEOS
        <?php wp_reset_postdata(); ?>
    <!-- end tab video -->
<!-- // end Front Page contents - BLOG-->
<!-- // start Front Page contents - GALL-->
    <!-- // Define our WP Query Parameters-->
    <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'gall' ) ); ?>
    <!--// Start our WP Query-->
        // here is included the Loop for - GALLERIES
    <?php wp_reset_postdata(); ?>
<!-- // end Front Page contents - GALL-->
<!-- // start Front Page contents - BOOK-->
    <!-- // Define our WP Query Parameters-->
    <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'download' ) ); ?>
    <!--// seleziona risultati per categoria - BOOK -->
        // here is included the Loop for - DOWNLOADS
    <?php wp_reset_postdata(); ?>
<!-- // end Front Page contents - BOOK-->
<!-- // start Front Page contents - EVEN-->
    <!-- start tab EVENTI FUTURI -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'event','scope' => 'future' ) ); ?>
        <!--// Start our WP Query-->
            // here is included the Loop for - Incoming Events
        <?php wp_reset_postdata(); ?>
    <!-- end tab EVENTI FUTURI -->
    <!-- start tab EVENTI PASSATI -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'event','scope' => 'past' ) ); ?>
        <!--// Start our WP Query-->
            // here is included the Loop for - Past Events
        <?php wp_reset_postdata(); ?>
    <!-- end tab EVENTI PASSATI -->
<!-- // end Front Page contents - EVEN-->
like image 714
Krnell Avatar asked Nov 06 '22 12:11

Krnell


1 Answers

Yes, you have to query everything on separate ways if they are in different post types.

In the blog categories case you can do something like:

$pp = 3;
$cats = ("post","rece","gall",...);

foreach($cats as $cat){
    $the_query = new WP_Query( array( 'posts_per_page' => $pp,'category_name' => $cat ) );
    //Loop in here
}

This way you can save lines and time, besides you can apply same logic on post type queries if they are the same post type.

Let me know if this works for you!

like image 132
José Lopez Coronado Avatar answered Nov 14 '22 21:11

José Lopez Coronado