Skip Navigation

[Resolved] I need to adjust a query filter to test if a Custom Field is not set.

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1179243

I am trying to adjust a Query Filter to test if a Custom Field is not set. However, I cannot seem to get this to work. Is there anything you recommend?

#1179259

There's not a way to accomplish this simply in wp-admin, but I can show you how to set up some custom code to handle this for you. First, remove any Query Filter you have applied to test the custom field value in the View editor. Then add this custom code to your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom Code:

add_filter( 'wpv_filter_query', 'get_books_without_zip', 10, 3);
function get_books_without_zip( $query_args ,$view_settings, $view_id ) {
    $views = array( 1234 );
    if (in_array($view_id, $views)) {
      $args = array(
        array(
          'key' => 'wpcf-your-field-slug',
          'compare' => 'NOT EXISTS',
          'value' => '',
        )
      );
       $query_args['meta_query'] = isset($query_args['meta_query']) ? $query_args['meta_query'] : [];
      $query_args['meta_query'][] = $args;
    }
    return $query_args;
}

Replace 1234 with the numeric ID of this View, and replace your-field-slug with the slug of your custom field as shown in wp-admin. Keep the wpcf- prefix. So if your field slug in wp-admin is "shirt-size" then you would use "wpcf-shirt-size" as the key.

#1181575

Thanks Christian. I ended up just setting up a new sub-category and assigning the product accordingly. This way I was not overriding functionality on the site. This is now working properly in my view.

However, one thing I did notice that the new sub-category is not showing up in the drop-down within my custom search. I have previously added other sub-categories and they show up fine, but for some reason this new one is not.

You can see the search page here... hidden link

There should be a sub-category for "Pigments". But it is not displaying. I know that it is indeed working as you can see the view here... hidden link

Any ideas on what might be causing this?

Appreciate the help!
Chris

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/filter-options-do-not-appear-in-custom-search-view/

#1181641

Let's address that issue in a separate ticket, since it's a bit different from the original problem here. Thanks for helping us keep things organized 🙂