0

I have a handful of products in my WooCommerce Store set to Catalog & Search and a bunch more just set to a visibility of just Search.

I put a lot of work into making sure my Catalog was nice and clean, but now I need to do some additional work on them and am trying to retrieve the IDs of products that are ONLY in my Catalog and not just search.

I've tried a few things, but something is amiss somewhere.

What am I missing?

$params = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'orderby' => 'menu-order',
    'order' => 'asc',
    'fields' => 'ids',
    'meta_query' =>
    array(
        array(
            'taxonomy' => 'product_visibility',
            'value' => 'exclude-from-catalog',
            'compare' => '!=')
        ));
$wc_query = new WP_Query($params);
$ids = $wc_query->posts;
echo '<pre>';
print_r($ids);
echo '</pre>';

With this Query it's still returning all Product IDs.

I'm assuming there must be something wrong with my meta_query argument. What I was going for was to look for the product_visibility taxonomy and exclude (!=) those that have the value exclude-from-catalog.

Anyone know where I'm going wrong with this?

2 Answers 2

0

Posted too quick. Found a solution shortly after

This works:

$params = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'orderby' => 'menu-order',
    'order' => 'asc',
    'fields' => 'ids',
    'tax_query' =>
    array(
        array(
            'taxonomy' => 'product_visibility',
            'field' => 'term_id',
            'terms' => 7,
            'operator' => 'NOT IN')
        ));
$wc_query = new WP_Query($params);
$ids = $wc_query->posts;
echo '<pre>';
print_r($ids);
echo '</pre>';

note the term id 7 is exclude-from-catalog

0

So your problem is that you're using meta_query and passing the parameters of a tax_query. You have to change your code to this if you're searching for a meta value:

$params = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'orderby' => 'menu-order',
    'order' => 'asc',
    'fields' => 'ids',
    'meta_query' =>
    array(
        array(
            'key' => 'product_visibility',
            'value' => 'exclude-from-catalog',
            'compare' => '!=')
        ));
$wc_query = new WP_Query($params);
$ids = $wc_query->posts;
echo '<pre>';
print_r($ids);
echo '</pre>';

or this, if its a term

   $params = array(
        'posts_per_page' => -1,
        'post_type' => 'product',
        'orderby' => 'menu-order',
        'order' => 'asc',
        'fields' => 'ids',
        'tax_query' =>
        array(
            array(
                'taxonomy' => 'product_visibility',
                'field' => 'slug'
                'terms' => 'exclude-from-catalog',
                'compare' => 'NOT IN')
            ));
    $wc_query = new WP_Query($params);
    $ids = $wc_query->posts;
    echo '<pre>';
    print_r($ids);
    echo '</pre>';

See more about tax_query and meta_query.

0

Your Answer

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

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