Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a simple loop, that gets the latest post of 3 selected categories. I've searched for something similar to learn from, but most are overly complex.

At the moment, I have:

    <?php 

    // WP_Query arguments
$args = array (
    'category_name'          => array('lifestyle', 'fashion', 'beauty')
);

// The Query
$query = new WP_Query( $args[0] );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

?>

I assume I'd need a foreach loop, but I'm unsure how to implement it in this scenario, and for just the latest of each post?

Any help would be great.

share|improve this question
    
This may one of the "overly complex" solutions you found but I disagree. It isn't overly complex, it is exactly (pretty close) as complex as needed to pull data in the way you describe. –  s_ha_dum Feb 24 at 17:46

1 Answer 1

It's not possible to get one posts per category with one simple query, and even a complex query will take more time than 3 separate query. So, if you want simplest, then this is the solution -

$cats = array('lifestyle', 'fashion', 'beauty');
$exclude_posts = array();
foreach( $cats as $cat )
{
    // build query argument
    $query_args = array(
        'category_name' => $cat,
        'showposts' => 1,
        'post_type' => 'post',
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'DESC'
    );

    // exclude post that already have been fetched
    // this would be useful if multiple category is assigned for same post
    if( !empty($exclude_posts) )
        $query_args['post__not_in'] = $exclude_posts;

    // do query
    $query = new WP_Query( $query_args );

    // check if query have any post
    if ( $query->have_posts() ) {

        // start loop
        while ( $query->have_posts() ) {
            // set post global
            $query->the_post();

            // add current post id to exclusion array
            $exclude_posts[] = get_the_ID();


            // do something
        }
    } else {
        // no posts found
    }

    // Restore original Post Data
    wp_reset_postdata();
}
share|improve this answer
    
That's fantastic. What does adding the current post id to the exclusion array do? Is that not too late, if it is inside the while() loop? –  tmyie Feb 24 at 18:19
    
No, we need it to be inside the loop. By current post id, i do not mean the page where you are executing this query. The exclusion is useful to return unique post per category. Example: if the latest post has cat A, B, it will only be showing in one of the category, not both of them. And the second category will look for the second latest post. –  Shazzad Feb 24 at 18:22
    
Thanks. My last question: does the if statement say exclude posts if not empty, and if so, is it not just calling an empty array? :) –  tmyie Feb 24 at 18:29
    
Yes, exactly right. –  Shazzad Feb 24 at 19:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.