The following code is supposed to get posts that do not have specific terms in a custom taxonomy. At the moment it still gets them. Is something missing.
$args = array(
'numberposts' => '3',
'post__not_in' => $post_not_in,
'tax_query' => array(
'taxonomy' => 'topic',
'terms' => 9,
'field' => 'id',
'operator' => 'NOT IN'
)
);
$extras = get_posts($args);
Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays)
— Wordpress Codex on Taxonomy Parameters
Have you tried?
$args = array(
'numberposts' => '3',
'post__not_in' => $post_not_in,
'tax_query' => array(
array(
'taxonomy' => 'topic',
'terms' => 9,
'field' => 'id',
'operator' => 'NOT IN'
)
)
);
$extras = get_posts($args);
Related
I have the problem not to get the meta_query working.
Category 5 are ContactPersons with an ACF 'bereich' for a category.
This is the code:
$cat=wp_get_post_categories( get_the_ID(), array( 'fields' => 'ids' ) );
array_pop($cat);
$c = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'category' => 5,
'meta_query' => array(
array(
'key' => 'bereich',
'value' => $cat,
'compare' => '='
)
)
));
}
field 'bereich' is an array of taxonomy category and same looking the $cat. The array_pop … I know the system 2 elements, just need one than its the same as 'bereich'.
LIKE brings me all of the parent.
IN also doesn't work.
I also tried this:
$c = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'category' => 5,
'tax_query' => array(array('taxonomy' => 'category', 'field' => 'slug', 'terms' => $cat)),));
}
It does not work!
Please help,
Tom
I am trying to get multiple taxonomy terms in WP_Query. Here is my code:
$category = get_field('portfolio_category'); //array of IDs like 14, 15, 16
<?php
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => array( implode(', ', $category ) ),
'operator' => 'AND'
)
),
));
?>
The problem is in the current code I can only query the very first term.
For example I can only get projects from cat ID=14.
What am I doing wrong here? How can I query posts from multiple terms?
Thank you.
Since $category is already an array... change your terms value to just $category. so your full query would be:
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => $category,
'operator' => 'AND'
)
),
));
Let's say I'm querying two post types, like so:
$args = array(
'post_type' => array('post', 'another_post_type'),
'tax_query' => array(
array(
'taxonomy' => 'custom-taxonomy',
'field' => 'slug',
'terms' => 'test-slug',
)
)
);
If I have custom-taxonomy linked to another_post_type, how would I run this query so that the tax_query only ran for the another_post_type posts? Basically I want the query to return all regular post, but only another_post_type posts with the category of test-slug.
Is this possible?
This seems to be working:
$args = array(
'post_type' => array('post', 'another_post_type'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'custom-taxonomy',
'field' => 'slug',
'terms' => 'test-slug'
),
array(
'taxonomy' => 'category', # default post category
'operator' => 'EXISTS'
)
)
);
The code below is for a taxonomy template (taxonomy-city.php), within each post I have two taxonomies City and Region.
I'm trying to show posts that share the same region as the city. eg if I'm on the London page I want to show other cities that have the Europe taxonomy term selected.
<?php
$today = date('Ymd');
$term = $wp_query->queried_object;
$getterm = $term->slug;
$args = array(
'posts_per_page' => '9',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $getterm,
'include_children' => true,
'operator' => 'IN'
),
array(
'taxonomy' => 'region',
'field' => 'slug',
'terms' => array( 'europe', 'asia-pacific', 'north-america' ),
)
),
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $today,
)
),
);
$query = new WP_Query( $args );
?>
You haven't actually said what the problem is. However, if you want the search to be, eg "London" in "Europe", then your relation should be set to AND. Also, you should add 'operator' => 'IN' to your region tax parameters. I know it's the default setting, but it doesn't hurt to explicitly declare it.
I would like to get results from two post types,
1) Post, only show 'business' kicker
2) local-news, show all kickers
I've so far:
$args=array(
'cat' => $my_category_id,
'post_status' => 'publish',
'posts_per_page' => 5,
'post_type' => array('post', 'local-news'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'postkicker',
'term' => 'business'
),
array(
'taxonomy' => 'impactkicker',
),
),
'orderby' => 'date',
'order' => 'DESC'
);
Currently is not showing both post types, any suggestions? Thanks in advance
You need to make sure you've connected your cpt local-news to category taxonomy as well, because you are trying to select by this taxonomy. Here's extensively commented approach which will do what you need i guess. At least if I got your idea clearly. You can change tax_query main relation to OR instead of AND to output items if they don't have category set as well.
$args = array(
// 'cat' => $my_category_id, // better replace it with category in tax_query, see below.
'post_status' => 'publish',
'posts_per_page' => 5,
'post_type' => array( 'post', 'local-news' ),
'tax_query' => array(
'relation' => 'AND', // we set it to AND because we want all posts of this category i guess.
array(
'taxonomy' => 'category',
'term' => $my_category_id,
'field' => 'term_id',
'operator' => 'IN', // just to be more explicit.
),
array( // we create nested sub queries which will filter by other 2 taxonomies, which in turn has OR relation.
'relation' => 'OR',
array(
'taxonomy' => 'postkicker',
'field' => 'slug', // by default it's term_id and you are passing in a slug so set it explicitly.
'term' => 'business',
'operator' => 'IN', // just to be more explicit.
),
array(
'taxonomy' => 'impactkicker',
'field' => 'slug', // set these or not add rule for taxonomy at all.
'term' => 'your-term-slug', // same here.
'operator' => 'IN', // it's a default value, but ou can set 'EXISTS' if you need to check if whatever term of such taxonomy is assigned.
),
),
),
'orderby' => 'date',
'order' => 'DESC',
);