I need to add related posts to my custom post type (using current taxonomy term). I know how to display them, I just need to be able to retrieve the currently selected taxonomy terms.
Here's what I'm doing:
$args = array(
'post_type' => 'mycustomposttype',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'thetaxonomy',
'terms' => 'theterm',
'field' => 'slug',
)
),
);
I need to dynamically populate "thetaxonomy" and "theterm" based on what's currently selected on the page. Getting the term is easy enough, but I'm struggling to get "thetaxonomy" slug.
Thanks in advance!
FIGURED IT OUT!
In case anyone is looking to accomplish the same thing
$currentTax = get_the_taxonomies($post->ID);
foreach($currentTax as $slug => $tax) {
$currentSlug = $slug;
};
$theTerms = array();
$term_list = wp_get_post_terms($post->ID, $currentSlug, array("fields" => "all"));
foreach($term_list as $term_single) {
array_push($theTerms, $term_single->slug);
}
So the query would look like this...
$args = array(
'post_type' => 'mycustomposttype',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => $slug,
'terms' => $theTerms,
'field' => 'slug',
)
),
);
Related
First time than I use Timber and I'm looking for help to find a way to filter some custom_post with a taxonomy.
In this file single-news.php, I try to display in my custom posts (news) controller a link with a an acf taxonomy field named 'categorie_dexpertise'
When I get it I use it in tax_query ...
I try to filer 'collaborateur' post type with term_id 'categorie_dexpertise' in 'expertise-collaborateur' taxonomy
But it doesn't work!
Single-news.php
<?php
$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post;
$context['term'] = new Timber\Term('category-news', 'news');
$expertise_team = get_field('categorie_dexpertise');
$context['expertise_team'] = $expertise_team;
$args = array(
'post_type' => 'collaborateur',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
'taxonomy' => 'expertise-collaborateur',
'field' => 'term_id',
'terms' => array(11),
),
);
$context['teamfilter'] = Timber::get_posts($args);
Timber::render('single-news.twig', $context);
Is there a better way or an alternative way to filter my custom-posts?
Thank for your help
:) :)
The tax_query parameter should always be an array of arrays. Try to use this code instead:
<?php
$args = array(
'post_type' => 'collaborateur',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'expertise-collaborateur',
'field' => 'term_id',
'terms' => array(11),
)
)
);
I want to use Advanced Custom Fields to determine from which category products will be shown. The ACF field gives the ID of the category and from that I can get the slug. I can't get it to work in the array though, and so it shows all products on the page. Does anyone have suggestions or ideas why it's not working? Thanks in advance!
$term_id = get_field('kies_product_categorie'); //Get category id
$term = get_term_by('id', $term_id, 'product_cat'); //Get terms from given category
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $term->slug //Slug of given category
)
)
);
Looking at the documentation here, you appear to be missing the field attribute within the tax_query array.
So your $args should look like:
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $term->slug, //Slug of given category
'field' => 'slug'
)
)
);
I have this query based on WordPress docs, but no result. If I remove the tax_query it gives all publish posts, but when I add it, it displays no result.
I have checked the database and I'm sure there is post attached to the wp_term_taxonomy so I'm not sure why it doesn't display results.
What I am doing here, I get all custom taxonomy type then loop through it to get all posts related to it.
$event_terms = get_terms(
'event_type',
array(
'orderby'=>'name',
'hide_empty'=>true
)
);
if(!empty($event_terms) && !is_wp_error($event_terms)){
foreach($event_terms as $event_term){
// code for each event term
$args = array(
'status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'event_type',
'field' => 'slug',
'terms' => $event_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$event_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
// this displays all taxonomy terms
echo $event_term->name."-".$event_term->term_taxonomy_id."<br>";
}
}
Please change your text query like this and try:
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'event_type',
'field' => 'slug',
'terms' => $event_term->slug
)
)
) );
$count = $the_query->found_posts;
I hope this is helps you.
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
)
));
I would like to create a foreach loop for taxonomy terms which is for custom post type.
More specifically I want a loop that queries all the products categories, but not the category "special-offers" and not categories subcategories. Bonus would be if, product has no category query them too and order all of them in ASC order (Not like sort products and categories separately. All of them must be sorted at the same time).
So what should I do with my code to make it work as needed?
Current code:
<?php
$args = array(
'post_type' => 'products',
'showposts' => -1,
'post_status' => 'publish',
'parent' => 0,
'hide_empty' => true,
'tax_query' => array(
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => array( 'special-offers', 'other-terms' ),
'operator' => 'NOT IN',
),
);
$terms = get_terms('categories', $args );
foreach ( $terms as $term ) :
echo '<h2>' . $term->name . '</h2>';
endforeach;
?>
Your Tax Query is should be looking within another array.
'tax_query' => array(
array(
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => array( 'special-offers', 'other-terms' ),
'operator' => 'NOT IN',
)
),
Rest of it seems okay.
Check out the WP_Codex on this
Final solution was to add exclude and term id to the taxonomy arguments. Since it is for taxonomy and it uses foreach loop.
$args = array(
'parent' => 0,
'hide_empty' => true,
'exclude' => 13,
);
And answer for how to output custom post type posts with no taxonomy can be found here: http://www.codeforest.net/wordpress-tip-show-posts-no-category-term
Thanks to CBroe and ste for their time.
Just incase someone runs into this in the year 2022 haha
$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );