I just bump into this… bug? I need to run a query to bring posts belonging to multiple categories, they need to have each category selected, so the operator needs to be “AND”. The problem is, I also need to bring the posts that belong to any sub category of a category selected, and this operator is preventing that to happen, even with “include_children” set to true.
Does anyone know a workaround for this situation?
Here’s a sample of my arguments array:
[post_type] => product
[posts_per_page] => 20
[paged] => 0
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => category
[terms] => Array
(
[0] => 300
[1] => 594
)
[operator] => AND
[include_children] => 1
)
)
[orderby] => name
[order] => ASC
Thanks!
]]>I’m trying, on one page of my site, to show only posts which are saved in the ‘video’ post format (I know this is not the ideal taxonomy for this, but I’m working with a legacy site and I can’t really change it). My theming skills are very very rusty, and I’ve been attempting to do it with taxonomy queries but whatever I do it seems to show every post in the archive regardless of format, rather than only the video format ones. What’s wrong with my code, and what do I need to change?
<?php
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-video'
)
)
);
query_posts();
while ( have_posts() ) : the_post();
the_content();
endwhile; // End of the loop.
?>
]]>I’d love some guidance.
I created 2 custom post types: ‘speakers’ and ‘events’. On my Events page is a nested loop of speakers using new WP_query. Works great! Except that for each Event I want to list a different group of speakers. It now shows ALL the speakers on each event page.
So I attached a custom taxonomy called ‘event-names’ with the terms ‘cooking’, ‘organizing’ and ‘budgeting’ to both speakers and events. I attached the appropriate term to each speaker and event. I cobbled together the code below, but not sure it this is correct, and if it is where I should put it. Any help will be greatly appreciated!
// speakers attached to the event-name 'cooking'
if ( has_term( 'cooking', 'event-names' ) ) {
// speakers attached to the event-name 'organizing'
} elseif ( has_term( 'organizing', 'event-names' ) ) {
// speakers attached to the event-name 'budgeting'
} elseif ( has_term( 'budgeting', 'event-names' ) ) {
// event name not specified
} else {
//not sure what to do here or where to place this code in relation to the above speaker query
}
]]>I’m really stuck on a problem and I’m not understanding it at all. I’ve read up on how to use taxonomy queries and it sounds like based on my data structure, this should work:
I want to show all equipment items that have a category slug = ‘type1’ and a location slug = ‘location1’. Here is how my query is structured:
$location = 'location1';
$type = 'type1';
// Build the args
$args = array(
'post_type' => 'equipment',
'orderby' => 'ID',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $type )
),
array(
'taxonomy' => 'locations',
'field' => 'slug',
'terms' => array( $location )
)
)
);
// Query the posts
$posts = query_posts( $args );
The issue here is that the query returns ALL of the equipment items that belong to the location given, but it ignores the category “type” specification. I can’t figure out why because based on all of the examples I’ve read, this should work just fine.
Any ideas? Thanks in advance!
]]>I have to made an attachment archive by custom taxonomy.
I don’t know why the page of the term, like mysite.com/custom_taxonomy/attachment_term, doesn’t work.
Anyway I decide to write a custom query by myself and run it inside a template page.
It works, but I have a problem.
When I get the attachments, I get all the of them, even if the parent is not published. This cause a 404 if someone clicks on them. Instead, if the post_parent is 0, with a click we arrive to the attachment page mysite.com/?attachment_id=x. Moreover if the attachment is regularly inside a post, it goes to mysite.com/post_name/attachment.
So how can I check all of these and get the attachments with parent 0 or the parent Publish?
Here’s the query right now
$sql = "
SELECT p.*
FROM $wpdb->posts p
LEFT JOIN $wpdb->term_relationships r
ON r.object_id = p.ID
LEFT JOIN $wpdb->term_taxonomy t
ON t.term_taxonomy_id = r.term_taxonomy_id
LEFT JOIN $wpdb->terms te
ON te.term_id = t.term_id
WHERE te.slug = 'OneOfmMyTerms'
";
thanks!!!
]]><form role="search" method="get" id="searchform" action="<?php echo bloginfo( 'url' ); ?>">
<input type="hidden" name="" value="" id="" />
<?php echo my_dropdown('genre', 'genre'); ?>
<?php echo my_dropdown('type', 'type'); ?>
<?php echo my_dropdown('ayear', 'ayear'); ?>
<input type="submit" id="searchsubmit" value="Submit" />
</form>
When submitting the form, it’s returning the ID’s rather than the slugs. This is my function,
<?php
function my_dropdown($name, $taxonomy = 'category') {
$defaults = array(
'taxonomy' => $taxonomy,
'id' => $name,
'name' => $name,
'show_option_none' => ' - Select - ',
'selected' => get_query_var($name)
);
wp_dropdown_categories($defaults);
}
add_action('pre_get_posts', 'my_customsearch');
function my_customsearch($query) {
if(! $query->is_main_query() || ! $query->is_search )
return;
$tax_query = array();
$genre = (int) get_query_var('genre');
$type = (int) get_query_var('type');
$ayear = (int) get_query_var('ayear');
// first dropdown
if (! empty($genre) && $genre > 0) {
$tax_query[] = array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => $genre
);
}
// second dropdown
if (! empty($type) && $type > 0) {
$tax_query[] = array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => $type
);
}
// third dropdown
if (! empty($ayear) && $ayear > 0) {
$tax_query[] = array(
'taxonomy' => 'ayear',
'field' => 'slug',
'terms' => $ayear
);
}
if ( sizeof($tax_query) > 0 ) {
$tax_query['relation'] = 'AND';
$query->set( 'tax_query', $tax_query );
}
}
// add my custom query vars
add_filter('query_vars', 'mycustom_query_vars');
function mycustom_query_vars($query_vars) {
$query_vars[] = 'genre';
$query_vars[] = 'type';
$query_vars[] = 'ayear';
return $query_vars;
}
?>
]]>I’m tearing my hair out here – I’m assuming I have a typo but not sure.
function cat_has_products($id) {
global $wpdb;
$id = trim($id);
$sql = "SELECT term_taxonomy_id from wp_term_taxonomy where term_id = $id;";
$termtaxid = $wpdb->get_var ($wpdb->prepare( $sql) );
echo "ID = $id <br>";
echo "Term id = $termtaxid <br>" ;
$catprodcount = $wpdb->get_var ($wpdb->prepare( "select count(object_id) from $wpdb->term_relationships where term_taxonomy_id = $termtaxid;") );
echo "Count is : <br>" . $catprodcount;
//return $catprodcount;
}
Typical output is as follows
ID = 96
Term id =
Count is :
where 96 is the value passed to the function.
]]>Using this method to get the posts where $taxonomy == ‘localities’ works in terms of correct posts being returned however it wont return excerpts at all.
Not sure if its related or points to something I have done wrong in setting up the Taxonomy but using the tax_query method fail to find any posts!
$paged = true by the way, though have tried false as well in case I had misinterpreted.
Select all
$args=array(
// 'tax_query' => array(
// array('taxonomy' => $taxonomy,
// 'field' => 'name',
// 'terms' => $local_name,
// )
// ),
'localities' => $local_name,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'paged' => $paged,
);
$temp = $wp_query;
//$wp_query = null;
$wp_query = new WP_Query($args);
Any thoughts would be much appreciated.
Cheers,
I’m trying to get my WP_Query to present the right set of posts, something must be wrong in my arguments since all kinds of posts get presented. Anyone with skills in taxonomy queries?
Basicly I have this: $myquery = new WP_Query($args);
a print_r($args) gives the following output:
Array ( [post_type] => Array ( [0] => myposttype ) [posts_per_page] => 16 [paged] => 1 [meta_key] => startDate [orderby] => meta_value [order] => DESC [tax_query] => Array ( [0] => Array ( [relation] => AND ) [1] => Array ( [taxonomy] => category [field] => id [terms] => Array ( [0] => 307 ) [operator] => NOT IN ) [2] => Array ( [taxonomy] => team [field] => id [terms] => Array ( [0] => 63 ) ) ) )
I would like to list custom posts (myposttype) with a certain taxonomy “team” id = 63 and exclude all posts from category “307”…
Did I append the different arguments wrong? Or this is not possible?
Thanks for your help!
//Mike