Navigation überspringen

[Gelöst] Using $query_args to filter for a taxonomy value

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to use wpv_filter_query to modify a View's query based on a taxonomy term slug.

Solution: It should work with slugs or names. Example with name to show only featured products:

add_filter( 'wpv_filter_query', 'featured_products',99,3 );
function featured_products( $query_args,$views_settings, $view_id) {
 
  if ($view_id == 10){
    $query_args = array(
      'post_type'  => 'product',
      'tax_query' => array(
        array(
          'taxonomy'     => 'product_visibility',
          'field'   => 'name',
          'terms' => 'featured',
          'operator' => 'IN'
        ),
      ),
    );
  }
  return $query_args;
}

Relevant Documentation: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created vor 5 Jahren, 6 Monaten. 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)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von alexG-4 vor 5 Jahren, 6 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1274933

I've written a Views filter that lets me dynamically filter Views based on posts having a specific tag value in a taxonomy.

After a bit of investigation, I determined that I need to use this structure within $query_args

[tax_query] => Array
(
[0] => Array
(
[taxonomy] => .... slug of taxonomy ...
[field] => id
[terms] => Array
(
[0] => ... ID of taxonomy term ...
)
[operator] => IN
[include_children] => 1
)
[relation] => AND
)

... and I got it working OK.

My question is about the [field] argument. Is there an option to use slugs instead of IDs for the taxonomy terms?

I would much rather use slugs to make my code more meaningful. I tried using 'field' => 'slug' and using slugs, but that didn't work.

Any way to do that?

Thanks

Alex

#1275181

Try using the field 'name' instead of 'slug'. Here's a filter query that only shows featured products (defined by a term in the product_visibility taxonomy):

add_filter( 'wpv_filter_query', 'featured_products',99,3 );
function featured_products( $query_args,$views_settings, $view_id) {

  if ($view_id == 10){
    $query_args = array(
      'post_type'  => 'product',
      'tax_query' => array(
        array(
          'taxonomy'     => 'product_visibility',
          'field'   => 'name',
          'terms' => 'featured',
          'operator' => 'IN'
        ),
      ),
    );
  }
  return $query_args;
}
#1275209

Doh!

I actually tried using 'name' as a guess, but it didn't work because of some other mistake I'd made: I just assumed my guess was wrong!

I tried to find documentation on this but couldn't find it: is there documentation available?

Alex

#1275211

Slug should work too, actually. The WP documentation for taxonomy parameters in a query is here: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

#1275221

Thanks, Christian.

Still learning about what's WP and what's Toolset!

Very helpful - thanks.

Alex