user15737027
user15737027

Reputation:

woo commerce shop page apply custom filter

I want to display particular authors' products on the shop page. but I don't know how can I do it I tried to do using the below code.

/**
   * Exclude products from a particular category on the shop page
*/
function custom_pre_get_posts_query($q)
{

 $user_id = 44;


$args = array(
    'post_type'      => 'product',
    'posts_per_page' => -1,
    'fields'        => 'ids',
    'author' => $user_id
);

$loop = new WP_Query($args);

echo '<pre>';
print_r($loop->posts);
echo '</pre>';



$tax_query = (array) $q->get('tax_query');

$tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array('camisetas'), // Don't display products in the clothing category on the shop page.
    'operator' => 'NOT IN'
);


$q->set('tax_query', $tax_query);
}
add_action('woocommerce_product_query', 'custom_pre_get_posts_query');

now I get all products of a particular user id in my case it 44. but I don't know how to implement it in woo-commerce tax query.

Upvotes: 2

Views: 620

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can pass author to query.

function custom_pre_get_posts_query( $q ){
    if( is_admin() ) return;

    $user_id = 44;
    $q->set( 'author', $user_id );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query', 10, 1 );

Upvotes: 1

Related Questions