Wordpress Tax Query Problem - How to query by taxonomy, specific parent AND child term? - php

I have a Custom Post Type called 'references' with the taxonomy 'references-cats'. Parent and child terms are:
social media
corporate communications
events
competences
social media
events
Some of some of the Child Terms are identical with the Parent Terms. I would like to query all posts of 'social media' (but only the children of 'competences') – is there an exact way to do that?
<?php
$tagz = get_the_title(); // single post title as we are on a single page
query_posts(array(
'post_type' => 'references',
'tax_query' => array(
'relation' => 'AND',
array (
'taxonomy' => 'references-cats',
'field' => 'slug',
'terms' => $tagz, // single post title corresponds with term
),
array(
'taxonomy' => 'references-cats',
'field' => 'slug',
'terms' => 'competences',
'operator' => 'IN'
)
),
'showposts' => 7
) );
?>

Terms in WordPress have unique slugs, so it doesn't matter if you have 2 terms with the same names, they will have different slugs. So you don't need to query the references by both parent and child since you are anyway using the slug to make the tax query.
'tax_query' => array(
array(
'taxonomy' => 'references-cats',
'field' => 'slug',
'terms' => $slug
)
)
The above piece of code is enough for the tax query, you should just use the proper slug for each taxonomy term in the $slug variable.
L.E.:
Matching your title with your term slug is not a good approach. You can achive what you want easily by creating a metabox on the page(with ACF of with custom code) where you select the category from which you want to display references on each specific page.

Solution:
Query for all posts with parent taxonomy. competences
Then in while loop check if post have child taxonomy.
Example:
$args_query = array(
'post_type' => array('references'),
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'references-cats',
'field' => 'term_id',
'terms' => array(PARENT-TAXONOMY-ID),
),
),
);
$query = new WP_Query( $args_query );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
if (has_term($tagz, 'references-cats')) {
// have $tags
}
}
}
wp_reset_postdata();
Also you can use below code instead of has_term()
if (is_object_in_term( $post->ID, $taxonomy, $term )) {
// code
}

Related

How to pass the Post Title as value?

I'm trying to pass the post title of a single post as value for 'terms':
<?php
$term = get_the_title();
query_posts(array(
'post_type' => 'referenzen',
'tax_query' => array(
array (
'taxonomy' => 'referenzen-kategorie',
'field' => 'slug',
'terms' => '{$term}',
)
),
'showposts' => 7
) );
?>
Explanation: the single post title corresponds to the terms of some Posts of the Custom Post Type 'referenzen', and there are several posts with the corresponding term for example: Design & Production --> I'm trying to pass the single Post Title "Design & Production" to 'terms' => 'Design & Production'.
The issue in the above code was passing post title instead of post slug in the terms field. You can get the post slug using the global $post variable which contains the complete post object array
<?php
global $post;
$term = $post->post_name;
query_posts(array(
'post_type' => 'referenzen',
'tax_query' => array(
array (
'taxonomy' => 'referenzen-kategorie',
'field' => 'slug',
'terms' => array($term),
)
),
'showposts' => 7
) );
?>

Show count of Custom Taxonomy only for a specific Custom Post Type

I want to display the count of a Custom Taxonomy based on a specific Custom Post Type. At the moment, I'm using get_terms to list all terms of the Taxonomy.
The Taxonomy is used by more than one Post Type. So the counter shows all the usage of the Taxonomy for every Post Type.
Is there a way to limit the count on a single Post Type?
Here is my actual code:
get_terms(array(
'taxonomy' => 'tax',
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
'number' => '10',
));
Inside the foreach, I'm using term->count to show the usage counter.
I wouldn't recommend using get_terms because this returns all terms of a taxonomy and not all terms associated with posts.
The alternative solution is to use get_posts, read more here https://developer.wordpress.org/reference/functions/get_posts/
$my_posts = get_posts(array(
'post_type' => 'post', //post type
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax', //taxonomy name
'field' => 'id', //field to get
'terms' => 1, //term id
)
)
));
Then you can count the number of posts returned:
$count = count($my_posts);
I think following link are more helpful to better understanding.
Link
And this is code serve for you.
$args = array(
'post_type' => 'Your_custom_post_type',//Your custom post type here.
'tax_query' => array(
array(
'taxonomy' => 'Your_taxonomy',//Your taxonomy is here.
'field' => 'slug',
)
)
);
Now we print_r the $args for the better understanding exactly what we get.
_e('<pre>');
print_r($args);
_e('</pre>');
Just get your query
$your_query = new WP_Query( $args );

How to use woocommerce data attributes in the loop

I have a custom product type and custom loop for listing woocommerce products
$query_args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'custom_type',
),
),
);
$r = new WP_Query( $query_args );
if ( $r->have_posts() ) {
I have custom data attributes in product datas. How can I use them in loop? How can I filter products with this attributes?
For example I have color and size data attributes. Now how can I the list red and large products?
Attributes are just custom taxonomies. Keep in mind that the taxonomy name will always be the attribute name preceded by pa_. This is just WooCommerce's naming convention to avoid conflicting taxonomy names. To query more than one taxonomy, see the "Multiple Taxonomy Handling" section in WP Query Parameters.
If, for example, you were trying to query products of product type = custom_type and color = red and a size attribute = large, your example args would look like this:
$query_args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'custom_type',
),
array(
'taxonomy' => 'pa_color',
'field' => 'slug',
'terms' => 'red',
),
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => 'large',
),
),
);

Sort by ACF option and also from current category - custom post type

With use of the Advanced Custom Fields plugin I created a select dropdown which contains 6 membership types. All of my 'listings' using this custom field are assigned one of the 6.
I have managed to get my listings to display in order of membership level, however it's not defining by category that your currently in. It's grabbing listings from every other category too.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'directory_listings',
'meta_key' => 'membership_type',
'orderby' => 'meta_value',
'taxonomy' => 'listing_category'
);
// query
$wp_query = new WP_Query( $args )
?>
<?php if (have_posts()) : ?>
<?php
while( $wp_query->have_posts() ) {
the_post();
ldl_get_template_part('listing', 'compact');
ldl_get_featured_posts();
}
?>
<?php else : ?>
<?php endif; ?>
Also, I am using the plugin: https://wordpress.org/plugins/ldd-directory-lite/
WP_Query does not have a taxonomy parameter, you should use tax_query instead. More info in the Codex.
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'slug',
'terms' => 'my-listing-category',
),
),
To grab the current taxonomy term dynamically (assuming you're on a listing_category taxonomy page):
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'term_id',
'terms' => get_queried_object_id(),
),
),

WooCommerce: function that returns all product ID's in a particular category

I think the title of the question is fairly self-explanatory, but to detail it...
I'm trying to make a custom category page in Woocommerce.
What I need is to return all the product ID's in a particular category.
I've seen this post, but it uses the WP_Query way of doing things, which is pretty ugly.
I'd prefer to use something in the WooCommerce classes to accomplish this.
Ideally, there should some sort of function/method like the following (but I can't find it):
get_products_in_category( $category_ID );
// Returns array of product ID's
Any help would be amazing.
Here you go:
function get_products_ids_from_category_by_id( $category_id ) {
$products_IDs = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category_id,
'operator' => 'IN',
)
)
) );
return $products_IDs;
}

Categories