Exclude Posts from Looper Dynamically

So, I’ve got a layout where I’d like to do the following:

  1. I want to display the latest post of the “Podcasts” category in the large section.
  2. I want to display the latest post of the “Articles” category in the top right smaller section.
  3. I will manually insert the latest series (it’s a different post type) in the bottom right small section.
  4. I would like then to display all remaining posts of all categories below.

I can easily complete steps 1-2 by creating their own looper providers for each using the query builder. However, when I want to do step 4, I cannot figure out how to automatically exclude the posts returned in steps 1-2. Any direction on how I might accomplish this? I know I could do an offset of 2, but that could end up problematic if I have posts that don’t fit into those two categories. Would the new Custom JSON Attributes in the query builder help at all with this (couldn’t find any documentation on this new feature)?

Is there a way to have a sub-looper that lets me loop through my loop and return certain posts by category?

Here is a screenshot of my sandbox page where I’m working on this:

Hello @bobbybosler,

Thanks for writing in!

Regretfully the new custom JSON Attribute feature in the Query Builder can not be used in your desired set up. You may need to have 3 separate Loopers to accomplish what you have in mind. The 3rd loop which will display the rest of the remaining items may use Looper Provider Custom that will require custom PHP coding or you just use Looper Provider Query Builder excluding the categories above.

Hope this helps.

Thanks, here is the code I ended up using for my custom looper (for anyone else trying to do the same thing).

// *** Custom Code to Create Homepage Custom Looper

// Function to retrieve the latest post IDs from specified categories
function get_latest_posts_from_categories($category_ids = array()) {
    $latest_posts = array();

    foreach ($category_ids as $cat_id) {
        $query = new WP_Query(array(
            'cat'              => $cat_id,
            'posts_per_page'   => 1,
            'fields'           => 'ids',
        ));

        if ($query->have_posts()) {
            $latest_posts[] = $query->posts[0];
        }
        wp_reset_postdata();
    }

    return $latest_posts;
}

// Custom Looper Provider to exclude the latest posts from specified categories
add_filter('cs_looper_custom_recent_posts_excluding_latest', function($result, $args) {
    // Get the latest posts from 'tg-podcast' (ID: 54) and 'articles' (ID: 1415)
    $latest_posts = get_latest_posts_from_categories(array(54, 1415));

    // Prepare query arguments to exclude the latest posts
    $query_args = array(
        'post_type'           => 'post',
        'post__not_in'        => $latest_posts,
        'orderby'             => 'date',
        'order'               => 'DESC',
    );

    // Execute the query
    $query = new WP_Query($query_args);

    // Return the posts
    return $query->posts;
}, 10, 2);

Glad to know that you were able to find a solution. Thank you for sharing.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.