23

The question was for Wordpress 3.0, but just in case someone has the same question for a more recent version, from Wordpress Codex: "Starting with version 4.1, meta_query clauses can be nested in order to construct complex queries." https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters So, that query should work on ...


18

tax_query takes an array of tax query arguments arrays (it takes an array of arrays) but you are using only single array. The correct code is as following. $uposts = get_posts( array( 'post_type' => 'product', 'numberposts' => -1, 'tax_query' => array( array( 'taxonomy' => $cat->taxonomy,...


15

You can define the args outside of the WP_Query instantiation: <?php $tax_query = array('relation' => 'AND'); if (isset($search_course_area)) { $tax_query[] = array( 'taxonomy' => 'course-area', 'field' => 'id', 'terms' => $search_course_area ); } if (isset($...


11

This can be done by using the term_taxonomy_id rather than the slug, which will effectively ignore whichever taxonomy is specified and just look at the unique term_taxonomy_id field. This will allow you to effectively be able to do a mixed relationship. You'd want to use an overall relation of AND and put all the terms that should be related OR in one item ...


10

not tested but give this a shot 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'image_tag', 'field' => 'term_id', 'terms' => 25, 'operator' => 'IN', ), array( 'taxonomy' => 'image_tag', 'field' => 'term_id', 'terms' => 41, 'operator' => '...


9

When you're doing a tax_query or meta_query in a WP_Query, you always have to use a nested array( array() ); just see the following example for an explanation and pay attention to the relation argument. $packages = new WP_Query( array( 'post_type' => 'vamos-cpt-packages', 'tax_query' => array( 'relation' => 'AND', array( ...


9

That seems to be impossible. Please someone correct me if I'm wrong. The meta_query parameter will actually be transformed into a WP_Meta_Query object, and the relation verification won't go deeper in wp-includes/meta.php, and occurs just once in the top level: if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) ...


9

It looks like you are making an array with a single string inside. Check if making $tax into an array before passing it will work: $tax = array( 19, 18, 214, 226, 20 ); $query_args = array ( 'post_type' => 'works', 'tax_query' => array( array( 'taxonomy' => 'materials', 'field' => 'term_id', ...


8

Unless you have posts that don't have a city aren't you essentially querying all your posts? Anyway, Eric Holmes is correct that you should be using WP_Query in lieu of query_posts(). That said, you can get a list of all the terms in a taxonomy and then use those values in your tax query. // get all terms in the taxonomy $terms = get_terms( 'city' ); // ...


8

Try This: $args = array( 'hide_empty' => false, // also retrieve terms which are not used yet 'meta_query' => array( array( 'key' => 'feature-group', 'value' => 'kitchen', 'compare' => 'LIKE' ) ), 'taxonomy' => 'category', ); $terms = get_terms( $args );


7

You can do this with just get_terms - this allows you to fetch all (or some) of the terms from one (or more) taxonomies. By default it excludes 'empty' terms, so you'll need to set the arguments appropriately. //Array of taxonomies to get terms for $taxonomies = array('category','post_tags','my-tax'); //Set arguments - don't 'hide' empty terms. $args = ...


7

First of all, you run register_post_type on init and register_taxonomy on after_setup_theme which is called after init. This means your custom taxonomy will not be available when registering the post type. I would suggest you remove the taxonomies keyword from the register_post_type arguments array, and just register the taxonomy manually afterwards. In your ...


7

Just providing an updated answer about Nested Taxonomies because they are indeed supported. The selected answer above is out of date / a little misleading. WordPress Core has supported Nested Taxonomies since v4.1 Taken from the WordPress Codex The 'tax_query' clauses can be nested, to create more complex queries. Example: Display posts that are in the ...


6

AFAIK $query->get for main query works only with public query vars, i.e. vars that can be triggered via url, but nothing prevents to directly access directly to tax_query property of query, but notice that it is an object, instance of WP_Tax_Query and the current queried taxonomy arguments are in the queries property of that object. Accessing to that ...


6

Your problem is that you are not resetting all the needed query variables, like WP_Query::$is_page, WP_Query::$is_single and WP_Query::$is_archive. Also note that 'pre_get_posts' is fired for all queries, the main and the secondaries, so you should check that you are working on the main query. Finally, when you get data from $_GETyou should sanitize it ...


6

I'm not sure if this is a bug, but it need further investigation. I've run a few quick tests on the name field in a tax_query, and whenever a term name has got a special character or have more than one word, the tax_query is excluded from the SQL query TEST 1 I have use two terms here, your term Ski-in/Ski-out and one of the terms on my test site Uit die ...


5

tax_query takes an array of arrays. You have an array of arrays of arrays. var_dump($tax_queries); and will get this: array(1) { [0]=> array(1) { [0]=> array(3) { ["taxonomy"]=> string(15) "difficulty_mode" ["terms"]=> NULL ["field"]=> string(4) "slug" } } } Try it without the square ...


5

The 0 = 1 happens when the terms don't exist. You're creating a query for some set of terms. Before it can add the SQL to that main query, it first does a secondary query to get the term_id's for the relevant terms. If none are found, then it returns 0 = 1 instead, to shortcut the main query because no terms match the request.


4

If you look at the documentation for WP_Query taxonomy parameters, you'll see that the tax_query is an array of arrays. So: 'tax_query' => array( array( 'taxonomy' => 'product-type', 'terms' => array('software'), 'field' => 'slug' ) )


4

The problem is explained in the quote you've mentioned: "Default value is 'publish', but if the user is logged in, 'private' is added. And if the query is run in an admin context, protected statuses are added too. By default protected statuses are 'future', 'draft' and 'pending'." Ajax call is always considered to be from an admin context.


4

Simply omit (or not add) tax_query part of arguments. $args = array( 'post_type' => 'wr_event', 'posts_per_page' => -1, 'meta_key' => 'event_date', 'orderby' => 'meta_value_num', 'order' => $order, 'meta_value' => $yesterday, 'meta_compare' => $compare, ); if ( ! is_null($cat) ) $args['tax_query'] =array( ...


4

The only option you have is to write your own SQL into the posts_clauses filter, where you get an array of the JOIN, WHERE, ORDER, etc. clauses that you can alter, add to, remove, etc. One MAJOR MAJOR note on this, is ALWAYS use the global $wpdb's prepare function, which will sanitize all your lovely datas. You don't want to be allowing any type of ...


4

Don't run a new query in the template, modify the main query before it's run via the pre_get_posts action in the theme's functions.php file. function wpd_author_query( $query ) { if ( $query->is_author() && $query->is_main_query() ) { // your code to set $current_user_name here $query->set( 'meta_key', '...


4

As already hinted by @Milo, check if you have terms before appending your tax_query You can try the following: (Requires PHP 5.4+ due to short array syntax, revert to old syntax if necessary) $args = [ 'post_type' => 'product', 'posts_per_page' => 15, 'paged' => $paged, 'post__not_in' => $exclude, 's' => $filter, ]; // ...


4

You cannot do this in one query, this is a bit too advanced for what WP_Query can do at this point in time. We will need to run at least two (I'm going to use 3) queries here to achieve what you want FIRST QUERY The first query will query for posts according to post_tag, that will also exclude the current post. We will only get post ID's SECOND QUERY ...


4

I suspect you need an array for the terms - although I'm not sure why it would work with "IN" and not with "NOT IN"... But I'd try this: function menta_pre_get_posts( $query ) { if ( !is_admin() && $query->is_search() && $query->is_main_query() ) { $term = get_term_by('slug', get_query_var('s'), 'product_tag'); if ( $term &&...


4

In a taxonomy template, the queried object is an instance of WP_Term, one of whose fields will be the taxonomy the term comes from. So, you can start your taxonomy.php as follows: $queried_object = get_queried_object () ; $args = array ( 'post_type' => 'inventory', 'tax_query' => array ( array ( 'taxonomy' => $...


4

AFAIK there's no way to achieve that within a single WP_Query, so you'll have to first get a list of term_ids which have a different year than the one in question. I think with the following you'll come quite close. (don't have a env to test right away) $other_years_terms = get_terms( 'taxonomy' => 'vintage', 'meta_key' => '...


3

You can't set query parameters after the query happens, set them before you make the query- $args = array( 'post_type' => 'articles', 'paged' => $paged, ); if( some condition ) $args['tax_query'] = array( your tax params ); $query = new WP_Query( $args );


3

This query will handle two levels of hierarchy in your taxonomy. More than two levels of hierarchy and you'll need a recursive self-join. What this does is return the posts in the correct child within parent order. To create the appropriate parent level headings, you'll have compare the current post's parent taxon with that of the prior post. Print the ...


Only top voted, non community-wiki answers of a minimum length are eligible