Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Amending WP_Query for a form with a dynamic variable

Viewing 9 posts - 1 through 9 (of 9 total)
  • realityhouse
    #213610

    Hi there,

    I have search and filter form where I need to set a custom meta query based on the values held within a dynamic array variable based on the user who is logged in (which controls what they see).

    So manually I tried the following:

    Post Meta > post_product = 1134

    Which is similar to what I want, and of course that works BUT I need the value of that query to be a dynamic array of product ID’s instead (held within $arr_product_ids in my example) depending on the user logged in, ie as follows:

    
       'meta_query' => array(
            'key'     => 'post_product',
            'value'   => $arr_product_ids,
            'compare' => 'IN',
        ),
    

    With that in mind, I have tried: https://searchandfilter.com/documentation/search-results/custom/ and added the following to my functions.php file but sadly this has not worked:

    
    
    global $arr_product_ids;
    
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 12,
        'category_name' => 'news',
        'meta_query'     => array(
            'relation' => 'AND',
            'post_product' => array(
                'key'     => 'post_product',
                'value'   => $arr_product_ids,
                'compare' => 'IN',
            ),
            'post_urgent' => array(
                'key'  => 'post_urgent',
                'compare' => 'EXISTS',
            ),
            'post_priority' => array(
                'key'  => 'post_priority',
                'compare' => 'EXISTS',
            ),
        ),
        'orderby'  => array( 'post_priority' => 'DESC', 'post_urgent' => 'DESC', 'date' => 'DESC' ),
    );
    $args['search_filter_id'] = 2978;
    $query = new WP_Query($args);
    
    

    I then looked at https://searchandfilter.com/documentation/action-filter-reference/ and tried adding the following to functions.php, but sadly again no luck:

    
    
    function filter_function_name( $query_args, $sfid ) {    
        if($sfid==2978) {
            $query_args['post_product'] = $arr_product_ids;
        }    
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    
    

    Please could you ket me know where I’m going wrong with this as I think I’m going around in circles sadly, whereas you will know the issue immediately I’m sure :).

    Many thanks,
    TJ

    Jordan
    #213626

    Hi TJ,

    On your second example, did you try adding: global $arr_product_ids within the function too? Your trying to use $arr_product_ids variable.

    
    function filter_function_name( $query_args, $sfid ) {    
        if($sfid==2978) {
    
            global $arr_product_ids;
    
            $query_args['post_product'] = $arr_product_ids;
        }    
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    
    Jordan
    #213628

    TJ, another quick example maybe to help:

    
    function filter_function_name( $query_args, $sfid ) {    
        if($sfid==2978) {
    
         global $arr_product_ids;
    
          $query_args['tax_query'] = array(
            array(
              'key'     => 'post_product',
              'value'   => $arr_product_ids,
              'compare' => 'IN',
            ),
          );
    
        }    
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    
    realityhouse
    #213642
    This reply has been marked as private.
    Trevor Moderator
    #213645
    This reply has been marked as private.
    realityhouse
    #213653
    This reply has been marked as private.
    Ross Moderator
    #213829

    Hi Holly

    I tried your code and it didn’t affect my setup at all (it should show no results, as that taxonomy doesn’t exist in my setup…)

    So… I had to double check the WP docs as it seemed like your tax query was ok on first glance but its not.

    I tried your code swapping out taxonomies for something I had in a testing environment and it didn’t work either (its much the same as yours):

    $query_args['tax_query'] = array(
    	array(
    	'key'     => 'cptuicat',
    	'value'   => array(9),
    	'compare' => 'IN',
    	)
    );

    I checked the docs and actually it should look like I think:

    $query_args['tax_query'] = array(
    	'relation' => 'AND',
    	array(
    		'taxonomy' => 'cptuicat',
    		'field'    => 'id',
    		'terms'    => array( 9 ),
    	),
    );

    This worked for me, the ID of 9 being a taxonomy term I have…

    If you copy and paste that, and swap out what you need then I think it should work.

    Let me know how you get on.

    Thanks

    Ross Moderator
    #213831

    Actually looking at your code, it looks like your structure is for a meta_query not a tax_query 🙂

    realityhouse
    #213848

    Hi Ross,

    Ahh thank you SO SO much for looking into this for me!!! So right on your last point, doh, so sorry!

    Now updated and all working a treat, I know I’ve said it before, but such an awesome plugin!!

    Thanks again 🙂

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Amending WP_Query for a form with a dynamic variable’ is closed to new replies.