WP_Query for getting posts under custom taxonomy not working - php

I am trying to get custom post types from a custom taxonomy. Following the documentation and other similar problems related here, i did the following:
$query = new WP_Query( array(
'post_type' => 'job',
'tax_query' => array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'california'
)
)
But the problem is that this query is getting all posts, not just posts under "California" taxonomy.
If more information is needed, i can provide more code editing the question.
Thanks in advance!

Try something like this:
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'job',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'california',
)
)
)
);

You're missing an array. The tax query argument can be used for multiple taxonomies. It accepts an array of arrays.
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.
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Change this:
'tax_query' => array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'california'
)
To this:
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'california',
),
),

In your while loop, remember to switch
<?php
while ( $query->have_posts() ) {
$query->the_post();
// Important line, especially if you have
//multiple WP_Query invocations
DO WHAT YOU WANT HERE
wp_reset_postdata();
}
?>

Related

Wordpress - Load pages filtering by parents id and taxonomies

I need to get the custom posts from a specific parent id and taxonomy.
I'm using this code, and I'm getting ALL the posts that are a child of the ID, but the taxonomy filter isn't affecting
<?php
$posts_array = get_pages(
array(
'post_type' => 'recetas_membresia',
'child_of' => $id_padre,
'tax_query' => array(
array(
'taxonomy' => 'comidas',
'field' => 'slug',
'terms' => $comida->slug
))
));
foreach ($posts_array as $post ) {;?>
<li><?php echo $post->post_title ;?></li>
<?php } ;?>
I have been tried to use compare in the query but still having the same result.
How can I get these specific posts?
From the get_pages function reference I can't see that tax_query would work here. (as it seems to work differently than all the other Post related functions. And I personally never used get_pages)
What you want to use is get_posts (ref).
Instead of child_of you'd use post_parent then to get the children, as listed in the WP_Query::parse_query ref.
$posts_array = get_posts(
array(
'post_type' => 'recetas_membresia',
'post_parent' => $id_padre,
'tax_query' => array(
array(
'taxonomy' => 'comidas',
'field' => 'slug',
'terms' => $comida->slug
)
)
)
);
posts_array = get_pages(
array(
'post_type' => 'recipes',
'child_of' => $id_parent,
'tax_query' => array(
'taxonomy' => 'food',
'field' => 'term_id',
'terms' => $food->term_id
)
));

WordPress: get queried object method always return null

I'm working a CMS based WordPress project and need some customization for search I mean custom search and for the terms I need get_queried_object and I have used like below
search.php
$object = get_queried_object();
var_dump($object);
// NULL
Edit search.php
I need terms from that query like below to use
$args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'project_category',
'field' => 'slug',
'terms' => array($object->slug)
)
)
);
I have tried this from SO but result is the same, what I'm doing wrong?
Thanks
(As discussed via the comments;) Use the get_search_query() function and not get_queried_object(), which always returns null on the search results page.
So here's the full code:
$search_term = get_search_query(); // The current search query/keyword.
$args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'project_category',
'field' => 'slug',
'terms' => array( $search_term )
)
)
);
As you need some customization for search, do utilize get_search_query() function instead.

WP query to get posts from category ( not child of it )

I have this $args for WP_Query:
$args = array(
'post_type' => 'estructura',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '12',
),
)
);
Witch returns the posts tagged with the category 12 or tagged with the childs categories ( of 12 )
I want only the ones tagged with category 12,
How can I prevent to return the childs?
Thanks!
I was able to solve this problem using the include_children option
$args = array(
'post_type' => 'estructura',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $tax_id, // 12
'include_children' => false
)
)
);
When you use tax_query, it's getting all of the children as they have the parent's url in their URL (e.g. website.com/category_name/child_1/).
$query = new WP_Query( array( 'cat' => 12 ) );
This should work fine. Source: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

Query posts from two post formats within a category

I have been reading the WP_Query Codex looking for a way to loop through all the posts that have the post format 'video' OR 'image', within a given category.
If this wasn't enough, this category is given by a variable $catslug (I need it to be this way).
I have only found ways of looping through one of the following
image OR video
image AND category
video AND category,
but what I need is more complex, something like this:
post-format-image AND $catslug) OR (post-format-video AND $catslug)
Is it possible to do a tax_query within a tax_query?
Something like this:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($catslug)
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-image' )
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($catslug)
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' )
)
)
)
);
$query = new WP_Query( $args );
Anyone knows any workaround or a hack to get this?
Perhaps I'm just thinking the wrong way.
This actually a good question. The simple answer here is that you cannot use multiple tax_query's.
This had me quickly testing the following scenario before I left for work. Just for fun I went and tried to make use of the category parameters with a tax_query, but that gave me posts from the desired category and posts that also belongs to both post formats
I came up with a possible solution, unfortunately I cannot test this right now.
Here is my line of though here:
As you need random results, I would suggest that you add your post formats into an array
$post_format_array = array( 'post-format-video', 'post-format-image' );
You now are going to use shuffle to shuffle the array, and then take the first entry from that array, which will be either video of image, and use that in your tax_query.
shuffle( $post_format_array );
This way you will get posts that is in your desired category and in either video or image post format.
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $catslug,
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_format_array[0]
),
),
);
$query = new WP_Query( $args );

Wordpress Query Posts Taxonomy

I currently have a checkbox based search and filter on my wordpress site.
Basically it works using this wp_query
$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>999999,
'category__and' => $_POST['myListOfCategories']));
I want to move away from using categories (as it's making using the blog a pain)
So I've set everything up using custom taxonomies now and notice there is no "taxonomy__and".
Does anyone know of a way to use WP_Query to search for posts using taxonomies in the same way category__and and works?
e.g. I pass multiple taxonomy id's and it only returns posts which have all of them linked.
From http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
You should be able to use something like this;
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' )
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
You have pass multiple taxonomy in Query_posts in Wordpress
See below URL It is very help full to you:-
https://wordpress.stackexchange.com/questions/25999/how-to-pass-url-parameters-for-advanced-taxonomy-queries-with-multiple-terms-for
https://wordpress.stackexchange.com/questions/10713/wp-3-1-getting-tax-query-to-work-in-query-posts
Query multiple custom taxonomy terms in Wordpress 2.8?
or try it
query_posts( array(
'tax_query' => array(
array(
'taxonomy' => 'tax1',
'field' => 'slug',
'terms' => array('term1', 'term2'),
'operator' => 'OR'
),
array(
'taxonomy' => 'tax2',
'field' => 'slug',
'terms' => array('term3', 'term4'),
'operator' => 'AND'
),
) );
With the help of the other posters here is the final answer to my question:
$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>10,'tax_query' => array(
array(
'taxonomy' => 'toy_cats',
'field' => 'id',
'terms' => array(14,20,39,42),
'operator' => 'AND' )
)));
The above code will only show posts that have the "toy" post type, is in the taxonomy "toy_cats" and is is assigned to ALL the following term id's 14 AND 20 AND 39 AND 42
Hope this helps someone else.

Categories