0

I have a custom post type, product with various categories. On the archive page for this post type, I would like to exclude the first 2 posts tagged with the "Featured" category (ID 7) from being displayed within the loop. All other products, including any other products tagged with "Featured," should show in the loop.

Here's where I'm at so far:

function exclude_first_two_from_cat($query) {

    if( !is_admin() && $query->is_post_type_archive('product') ) {
        // Get the IDs of the first 2 products with featured category
        $exclude_post_ids = get_posts(array(
            'posts_per_page' => 2,
            'cat'   => '7',
        ));
        $query->set( 'post__not_in', (array) $exclude_post_ids ); 
    }

}
add_action('pre_get_posts', 'exclude_first_two_from_cat', 50);

For some reason the get_posts call does not seem to be querying only the product post type. I tried adding 'post_type' => 'product' but that results in a 500 error (I'm assuming because I'm already using the is_post_type_archive conditional.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.