Can't get tax_query to work - wordpress

I have a custom WP Query where I list custom post types. I am trying to set up filters to filter listings by custom taxonomy.
My query setup is as follows:
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => $postsperpage,
'posts_per_archive_page' => $postsperpage,
'ignore_sticky_posts' => true,
'order' => 'DESC',
'orderby' => 'date',
'cache_results' => true,
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);
So in the above example I should get only posts with taxonomy 'sector' with slug 'business-services', but ALL posts from custom post types 'opps' get listed instead.
I have looked everywhere on stackoverflow and beyond but I just can't figure out what I've been doing wrong.

Your code looks fine.
I have had this issue before because wordpress does not sync taxonomy terms until late in the init phase. Have you tried running this code in a template, after get_header() or wp_head() has been called? If not try that, it should work then. I know if I've echoed it out in functions.php for testing it will not work because wordpress has not synced tax terms yet.

You should really have a look at the codex (WP_Query) when creating custom queries. As your code stands, it will fail. You have the following issues
pagination is not a valid parameter for WP_Query.
You cannot use posts_per_page and posts_per_archive_page together
You don't need to set parameter values that are set by default. Unnecessary parameters used in your code which is by default the default values are order, orderby, post_status, cache_results, update_post_meta_cache and update_post_term_cache
There is no sticky post functionality for custom post types. just remove the ignore_sticky_post parameter
Your code should look something like this ( I hope your variables is defined )
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'posts_per_page' => $postsperpage,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);

Related

WP_Query -- how to query for post ids or tag slugs

I'd like to create a WP query that allows me to look for posts with particular tag slugs or pages with particular ids.
Here's what I've come up with:
$query = new WP_Query([
'post_type' => array('any'),
'posts_per_page' => $posts_per_page,
'tag_slug__in' => $tags_as_array,
'post__in' => $post_ids_as_array
]);
But this doesn't work. It appears to work only when I remove the "tag_slug__in" key or the "post__in" key.
Is there a way to do this using WP_Query? I was hoping to not have to create a custom SQL statement.
tag_slug__in
only works for a blog post, not for custom post types or others. There is a reason that your query is not working correctly.
You need the tax query for custom post types or others.
Please follow this:
The WordPress Query class
$query = new WP_Query( [
'post_type' => [ 'any' ],
'posts_per_page' => $posts_per_page,
'post__in' => $post_ids_as_array,
'tax_query' => [
[
'taxonomy' => 'your-taxonomy-name',
'field' => 'slug',
'terms' => $tags_as_array,
'operator' => 'IN'
],
]
] );

Woocommerce Get all product variations by category

I need to get all Woocommerce product variations that belongs to selected category. I tried to query that this way:
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $selected_category_id
)
);
$variations = get_posts( $args );
but "tax_query" part seems to be ignored and no matter what will be provided inside, it does not affect the query.
Any ideas how to do that?
Tax Query Docs
Important Note: tax_query takes an array of tax query arguments arrays
(it takes an array of arrays). This construct allows you to query
multiple taxonomies by using the relation parameter in the first
(outer) array to describe the boolean relationship between the
taxonomy arrays.
I suggest you to use new WP_QUERY() instead get_posts() using WP_QUERY you can see query ran against your arguments via var_dump($variations->request);
$args = array(
'post_type' => 'product_variation',
'post_status' => array('private', 'publish'),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $selected_category_id
)
)
);
$variations = new WP_Query($args);
var_dump($variations->request); // dump SQL query

how build a wordpress query_posts based on url

I want to use the query_posts function from wordpress for displaying the right posts. I want to use the url www.site.com/?s=taxonomy1=test1&taxonomy2=test2 for building the query. something like down here:
$taxonomy1 = $_GET['taxonomy1'];
$taxonomy2 = $_GET['taxonomy2'];
query_posts(array(
'posts_per_page' => 10,
'taxonomy1' => $taxonomy1,
'taxonomy2' => $taxonomy2,
) );>
How i do this?
the wordpress codex is your best friend when trying to build custom queries. something like this should work as a query
$taxonomy1 = $_GET['taxonomy1'];
$taxonomy2 = $_GET['taxonomy2'];
$the_query = new WP_Query(array(
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $taxonomy1,
'field' => 'slug',
),
array(
'taxonomy' => $taxonomy2,
'field' => 'slug',
),
),
));
and to display the results
while ( $the_query->have_posts() ) : $the_query->the_post();
//the_title(), the_content(), etc
endwhile;
note that the query is using the new (as of 3.1) method for querying taxonomies. As you can see it gives you a lot more flexibility and i would suggest reading through the above link

Get multiple posts by multiple slugs

I have an array with multiple slugs (post names) and I want to get all posts where the slug is in the array.
I use Event Organsier and want to get all events these slugs. I haven't worked with WordPress for a couple of years so I need some help.
$events = eo_get_events(array(
'numberposts'=>5,
'event_start_after'=> $year.'-'.$month.'-'.$list_day,
'event_end_before'=> $year.'-'.$month.'-'.$list_day,
'showpastevents'=>false,//Will be deprecated, but set it to true to play it safe.
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array('slug-example-1', 'slug-example-2'),
'operator' => 'IN'
)
)
));
This should (imo) return all posts that has a slug that is slug-example-1 or slug-example-2.
I don't even know if tax_query/taxonomy is the right thing to use.
You can use the post_name__in argument in conjunction with get_posts(), it's available since Version 4.4.
E.g. :
$args = [
'post_name__in' => [ 'slug-1', 'slug-2' ],
'post_type' => 'page',
'post_status' => 'publish',
];
$result = get_posts( $args );

Query posts by custom taxonomy ID

I have a custom post type called portfolio and a custom taxonomy called build-type (acting as categories)
I am trying to query portfolio posts by build-type ID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id'
),
'orderby' => 'title',
'order' => 'ASC'
));
Currently it's calling all portfolio posts and not just those with the build-type ID
For 'field' => 'term_id' should I be using term_id, tag_ID, id or something else?
Anyone know how to get this working?
Thanks in advance!
I solved it with help from: https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id
tax-query needs to be an array of arrays
The final solution is:
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id',
)
),
'orderby' => 'title',
'order' => 'ASC' )
);
On github here:
https://gist.github.com/1275191
I'm not a WP-gury and I have invested hours and hours trying to solve the same problem. Eventually I found this blog post: http://richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/
The answer is somewhat semi-bad: apparently you can't filter like this for custom post types (it is only possible for posts), which is a shame!
What I did work was this:
$args['custom_tax'] = 'custom_tax_slug';
query_posts($args);
Hope it helps!
//Mike

Resources