WP Query to exclude category?

Community Forums Forums General Discussion WP Query to exclude category?

This topic is: not resolved

This topic contains 3 replies, has 2 voices, and was last updated by  ezines 11 months, 3 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #81974

    ezines
    Participant
    Post count: 74

    I’m a using following code to pull products into a homepage slider:

    $my_query = new WP_Query('posts_per_page=5&post_type=product');

    How do I exclude a specific category?

    #81979

    genwrock
    Member
    Post count: 937

    try this

    $args = array(
    'posts_per_page' => 5,
    'post_type' => 'product',
    'tax_query'	=> array(
            array(
                'taxonomy'  => 'product-cat', //PUT YOUT TAXONOMY
                'field'     => 'slug',
                'terms'     => 'media', // ADD YOUR TERMS SLUG
                'operator'  => 'NOT IN')
                ),
           )
    );
    $my_query = new WP_Query($args);

    See My PRO Genesis Child Themes | Need Genesis Developer? | Follow Me on Twitter

    • This reply was modified 11 months, 3 weeks ago by  genwrock.
    • This reply was modified 11 months, 3 weeks ago by  genwrock.
    #82212

    ezines
    Participant
    Post count: 74

    I’m a PHP newbie. No idea what to do with your code.

    Here’s full code snippet:

    <?php $slider_cat = 1; if($th_options['th_featured_carousel_cat'] != '') { $slider_cat = 
    implode(",", $th_options['th_featured_carousel_cat']); } 
    $my_query = new WP_Query('posts_per_page=12&post_type=product);
            while ($my_query->have_posts()) : $my_query->the_post();
            $image_id = get_post_thumbnail_id();
            $image_url = wp_get_attachment_image_src($image_id,'carousel');
            $image_url = $image_url[0]; ?>

    Where/how do I add new code?

    #82619

    ezines
    Participant
    Post count: 74

    Figured it out!

    In case someone else needs it:

    – changed approach to include instead of exclude, which didn’t work

    – cat parameter isn’t usable for products

    – used product_cat and category slugs to include all categories except ones to be excluded

    $my_query = new WP_Query('posts_per_page=12&post_type=product&product_cat=category-slug-1,category-slug-2,category-slug-3

    • This reply was modified 11 months, 3 weeks ago by  ezines.
    • This reply was modified 11 months, 3 weeks ago by  ezines.
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.