I am facing issue with a custom plugin, which will show related post by taxonomy from a particular category(mobile). If there are no post matching taxonomy, it should display other posts from that particular category. First I fetched all taxonomies and terms for the current post(single.php). Then I prepare a query argument using loop. The code works for the below cases,
a) I have not added any post tag(current post), then it's showing other posts from the same category(mobile),
b) If I have added a post tag, and there are other post matching the terms of that post tag.
But, it's don't work when I have added a post tag, and there are no post matching those terms. But, here I want, if there are no matching post by the terms, then just display other posts irrespective of terms. I can do this, using a new query when have_posts fails, but I am thinking if there are any other way to do this withing the same query args, please help
I am placing the code which I am trying to develop.
$post_args = array();
$taxonomies = get_post_taxonomies( $post );
foreach ($taxonomies as $key => $taxonomy) {
# code...
if($taxonomy == 'category') continue;
$terms = wp_get_post_terms( get_the_ID(), $taxonomy );
$term_array = array();
if($terms){
foreach ($terms as $key => $value) {
array_push($term_array, $value->slug);
}
array_push($post_args,
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term_array,
)
);
}
}
$tax_query = array();
$tax_query['relation'] = 'OR';
foreach ($post_args as $key => $value) {
# code...
array_push($tax_query, $value);
}
$args = array(
'post_type' => 'post',
// 'category_name' => 'mobiles',
'post__not_in' => array($curr_post_id),
'posts_per_page' => 10,
'orderby' => 'relevance',
'order' => 'ASC',
'tax_query' =>
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'mobiles',
)
,
array($tax_query)
)
);
$the_query = new WP_Query( $args );
After trying a lot, I just decided to use another post loop. Say, if there are no result from first loop, just select random post from the category. If there are few posts(Max 10 posts) from the first loop, then user another loop to select random posts excluding the post ids we get from first loop.
Related
I want to list a every taxonomy which is used in the output of a custom query.
For example:
I have a couple of cars listed on my site and a custom query to get all SUVs from that list. For that I'm using the following query args:
$args = array(
'post_type' => array( 'cars' ),
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'car_type',
'field' => 'slug',
'terms' => 'suv',
),
),
);
$query = new WP_Query( $args );
$posts = $query->posts;
foreach ( $posts as $post ) {
$term_objects = get_the_terms( $post->ID, 'brand' );
foreach ( $term_objects as $term_object ) {
$terms_list[ $term_object->name ] = $term_object;
}
}
With this query I get a list of all SUVs within my cars.
Now I want to show a list of brands. The brand is a also a custom taxonomy.
So let's say there are 50 cars as a result from my query from 5 different brands.
How could I filter my query result and get the IDs of every brand (only once per brand) within the query results.
Let's say I have a website which sells cars.
I'm using a custom taxonomy called brand for the manufacturer (like BMW, Audi, ...) and a custom taxonomy called type for the type of cars (like SUV, Coupe, ...).
For the cars itself I'm using a custom post type called models.
Now I want to show every car brand in the taxonomy archive for type (All brands with SUVs).
To do that, I'm trying to get all brands and filter them with all types.
As result there should be a list with all car brands that have SUVs.
Here's my current code to get a list of brands:
$taxonomies = get_terms( array(
'taxonomy' => 'brand',
'hide_empty' => false
) );
if ( !empty($taxonomies) ) :
$output = '<select>';
foreach( $taxonomies as $category ) {
if( $category->parent == 0 ) {
$output.= '<optgroup label="'. esc_attr( $category->name ) .'"></optgroup>';
}
}
$output.='</select>';
echo $output;
endif;
I couldn't find a way to add a second taxonomy to this snippet.
Is this the wrong approach?
Maybe I need to get the custom post types (models) first to check which one has both terms?
This article has a really helpful breakdown.
I needed to get the taxonomies that were part of another taxonomy for use in creating a dropdown (and some other uses.) This code allowed me to identify the "categories" that were part of a particular "destination".
$dest_slug = get_query_var('term');
$desination_ids = get_posts(array(
'post_type' => 'item',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'destinations',
'field' => 'slug',
'terms' => $dest_slug
)
),
'fields' => 'ids'
));
$categories = wp_get_object_terms($desination_ids, 'category');
I found a solution that works:
$productcat_id = get_queried_object_id();
$args = array(
'numberposts' => -1,
'post_type' => array('models'),
'tax_query' => array(
array(
'taxonomy' => 'brand',
'field' => 'term_id',
'terms' => $productcat_id,
),
),
);
$cat_posts = get_posts($args);
$my_post_ids = wp_list_pluck ($cat_posts, 'ID');
$my_terms = wp_get_object_terms ($my_post_ids, 'types');
if ( !empty($my_terms)) :
echo '<ul>';
foreach( $my_terms as $my_term ):
$brand_name = $my_term->name;
$brand_link = get_term_link($my_term);
echo '<li><a alt="'.$brand_name.'" href="'.esc_url( $brand_link ).'">'.$brand_name.'</a></li>';
endforeach;
echo '</ul>';
endif;
I am facing some issues with my custom taxonomies in one of my custom post types : I have a page that retrieves my custom posts that have the same taxonomy fields as the page.
For example, one of the custom taxonomies shared between the page and the CPT is 'categorie'. Using a tax_query field in a WP_QUERY, I am able to retrieve the posts that do have the same categorie fields checked as my page's, but I can't seem to find how to get posts that have the EXACT same values.
Here are some visuals in case you're confused :
Page taxonomy with values checked
CPT taxonomy with values checked
So they both have the 'chauffage' value checked and no other one.
Let's say now that I have an other custom post that has 'chauffage' checked, but also 'sanitaire'. How do I tell my WP_QUERY not to retrieve it ?
I've tried to use the operator 'AND' inside of my tax_query array (since it is 'IN' by default) but it didn't work.
The idea I had now was to retrieve all of the taxonomy values and say that the unchecked ones use the operator 'NOT IN', which would work I guess if only I managed to do so. I am only able to get the checked ones, even when using wp_get_post_terms with 'hide_empty' set to 0.
Here's my code :
$page_id = $wp_query->get_queried_object()->ID;
$page_category = wp_get_post_terms($page_id, 'categorie', array(
'hide_empty' => 0
));
// var_dump($page_category);
$page_client = wp_get_post_terms($page_id, 'client', array(
'hide_empty' => 0
));
if($page_category) {
$buffer = array();
foreach ($page_category as $category) {
array_push($buffer, $category->slug);
}
$page_category = $buffer;
}
// var_dump($page_client);
if($page_client) {
$buffer = array();
foreach ($page_client as $client) {
array_push($buffer, $client->slug);
}
$page_client = $buffer;
}
$tax_query = array(
'relation' => 'AND'
);
array_push($tax_query, array(
'taxonomy' => 'categ_article_activite',
'field' => 'slug',
'terms' => $page_category,
'operator' => 'AND'
));
// var_dump($page_client);
array_push($tax_query, array(
'taxonomy' => 'client_article_activite',
'field' => 'slug',
'terms' => $page_client,
'operator' => 'AND'
));
// var_dump($tax_query);
$args = array(
'post_type' => 'article_activite',
'meta_key' => '_article_principal',
'posts_per_page' => 1,
'meta_value' => 'on',
'tax_query' => $tax_query,
'order' => 'ASC'
);
$query = new WP_Query($args);
EDIT : I forgot to mention, I also thought about using the operator 'NOT IN' for the values I do not want to retrieve but since the people that are going to use the website may add taxonomy values, it would require to go in the code add this specific value which is not an option
I am facing a little problem whilst trying to display posts, from certain categories, on the front end. I've created a custom field whose key is 'related_categories' and I've different categories with slugs such as 'army', 'navy', 'airforce' etc.
When creating a new custom post, I enter the slugs of different categories inside the 'related_categories' custom field, for e.g. 'navy', 'airforce', and this is the code which I've inside my template file to echo the custom field values inside the 'tax_query' array.
<?php global $post;
$related = array ( get_post_meta($post->ID, 'related_categories', true) );
$the_query = new WP_Query( array( 'post_type' => 'medals', 'posts_per_page' => 3, 'tax_query' => array( array( 'taxonomy' => 'medal-categories', 'field' => 'slug', 'terms' => $related, ), ), ) );
while ( $the_query->have_posts() ) : $the_query->the_post();
//loop starts after this
Now the problem which I am facing is that if I enter only one slug inside the 'related_categories' custom field, for e.g. 'navy', then the posts from the 'navy' category successfully show up but if I enter more than one slug inside the 'related_categories' custom field, for e.g. 'navy', 'army', then the posts do not show up.
I tried searching for a solution on the internet but couldn't find any therefore any help would be appreciated. Thank you.
Please use this plug-in. I hope all of you problems will solve in this : https://wordpress.org/plugins/advanced-custom-fields/
In this u can easily assign which fields need to show in which category and pages.
Found a solution at last! Instead of using:
$related = array ( get_post_meta($post->ID, 'related_categories', true) );
I was advised to use:
$related = explode(',', trim( get_post_meta($post->ID, 'related_categories', true) ) );
along with adding the category slugs inside the custom field in the following format:
navy, army, air-force..
This is how the final code looks like:
<?php global $post;
$related = explode(',', trim( get_post_meta($post->ID, 'related_categories', true) ) );
$the_query = new WP_Query( array( 'post_type' => 'medals', 'posts_per_page' => 3, 'tax_query' => array( array( 'taxonomy' => 'medal-categories', 'field' => 'slug', 'terms' => $related, ), ), ) );
while ( $the_query->have_posts() ) : $the_query->the_post();
//loop starts after this
In my wordpress installation, I have a custom taxonomy event-categories which is mapped to custom post type event.
In my single post display page, I need to list all posts which is posted in same event-categories of the current post. How can I write a wp query for this?
Screen-shot for my custom taxonomy in custom post.
Now tried like this get_the_terms(the_ID(), 'event-categories').
So I got all term_taxonomy_ids related to the single post. Next how can I get all posts which have these term_taxonomy_id.
This would be about the most basic query to solve your problem.
$term_tax_ids = get_the_terms(get_the_ID(), 'event-categories');
$terms = array();
foreach($term_tax_ids as $term_tax_id) {
array_push($terms, $term_tax_id->term_id);
}
$args = array(
'post_type' => 'event',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'event-categories',
'field' => 'id',
'terms' => $terms,
'operator' => 'IN',
)
)
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="related_single">' . get_the_post_thumbnail();
echo '<div class="related_title">' . get_the_title() . '</div></div>';
}
You should really read the Codex, it has literally everything you could ever want to know about queries