skip to Main Content

I am using Elementor Anywhere to create a specific display for my WooCommerce products.

I have a filter and for the filter works I need to use Post blocks Adv.

There I ask to display current archive.

I have the possibility to put a query filter, but I can’t exclude products from a certain category.

I have this, but it doesn’t work with current archives.

function filtrer_produit_sans_crea( $query_args ) {
    $query_args['meta_query']  = array(
        array(
            'key'   => '_stock_status',
            'value' => 'instock',
        ),
    );

    $query_args['tax_query'][] = array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => 'coin-des-creatrices',
        'operator' => 'NOT IN',
    );

    return $query_args;
}

add_filter( 'filtre_zero_dechet', 'filtrer_produit_sans_crea' );

Has anyone ever tried something like this?
Or do you have a clue?

2

Answers


  1. Chosen as BEST ANSWER

    OK I found a solution:

    function mystore_pre_get_posts_query( $q ) {
    
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
               'operator' => 'NOT IN'
        );
        $q->set( 'tax_query', $tax_query );
    
    }
    add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );  
    

    But it does on the whole site.

    If anyone has a clue how to do it only on a specific page...

    Naively I wanted to add this:

    function mystore_pre_get_posts_query( $q ) {
    if ( is_page(  54 ) ) {
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
               'operator' => 'NOT IN'
        );
        $q->set( 'tax_query', $tax_query );
    }
    }
    add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );  
    

    But it's doesn't work ^_^


  2. I found another track.

    function mystore_pre_get_posts_query( $q ) {
    
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
               'operator' => 'NOT IN'
        );
        $q->set( 'tax_query', $tax_query );
    
    
    }
    
    
    
    add_action( 'wp_head', 'remove_my_action');
    
    function remove_my_action($post) {
        global $post;
        $post_id = $post->ID;
        if (160029  == $post_id) {
             echo "PAGE OK";
             add_action( 'pre_get_posts', 'mystore_pre_get_posts_query');
        }else{
            
            remove_action('pre_get_posts', 'mystore_pre_get_posts_query');
             
            
        }
    }
    

    My "page ok" displays well but the function does not launch.

    If it is outside this function, the line add_action('pre_get_posts', 'mystore_pre_get_posts_query');

    does what I want it to do.

    Is it add_action( 'wp_head', 'remove_my_action'); that is not the right one?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search