WP custom post type query does not return custom taxonomy - php

I have the following CPT:
array(
'slug' => 'articles',
'single_name' => 'Article',
'plural_name' => 'Articles',
'menu_name' => 'Articles',
'description' => '',
'dashicon' => 'dashicons-media-default'
),
and the following custom taxonomy which shows up under the articles CPT:
array(
'slug' => 'author',
'single_name' => 'Author',
'plural_name' => 'Authors',
'menu_name' => '→ Authors',
// This is where you sync the taxonomy to the post types above
'post_type' => ['articles', 'news']
),
Now, I am trying to create a single-articles.php in which I query a single article, to include all custom taxonomy information.
I've tried all variations of:
$args = array(
'post_type' => 'articles',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug'
),
),
);
I am relatively new to PHP & WP and am not 100% sure how to retrieve the data. In Javascript you would get an object back through which one can traverse. When adding print_r($my_query) I don't get what back what I am looking for. Earlier in the document I already initiated the loop:
if (have_posts()) :
while (have_posts()) : the_post()
but I can't figure out why php/wp doesn't show all data included in the object. Ideally, I want to query only the single article and get all data back, to include info about all attached taxonomies. I do not want to query articles BY the taxonomy So I have two questions:
1) How do I query this correctly to get results back?
2) How do I display this data on the frontend?
Edit: According to this tax_query is ignored when is_singular() is true. print_r( is_singular() ) results in 1 (aka true?!), would this make a difference?
I also have custom post types attached to the taxonomies. I need to retrieve this info as well.

Your taxonomy slug is author NOT authors
$args = array(
'post_type' => 'articles',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => 'your-term-here',
),
),
);
You can Use single-{posttype}.php for the single template.
And, if you have register your post type with the has_archive argument set to true, then you can use archive-{posttype}.php for your archive template.
You can check Template Hierarchy

Related

WP Query returning results even when $args are not met?

In the below WP Query, there seems to be a slightly odd behaviour. When the criteria is met, the correct products are shown, however, if not products match the criteria, other products are shown, rather than having no results? I'm unsure why this might be, but I would assume it is something to do with the tax_query section. In short, what it is trying to retrieve is products from the current tag, that are NOT tagged as featured products (I have another query that targets featured products so this is to avoid duplication).
How can I ensure no results are shown if the $args are not met?
Thanks.
//Get current Tag
$currentTag = get_term_by('slug', $wp_query->get_queried_object()->slug, 'product_tag');
$args = array(
'post_type' => 'product',
'status' => 'publish',
'fields' => 'ids', // Only return IDs
'orderby' => 'menu_order',
'order' => 'ASC',
'limit' => -1,
'tax_query' => array(
'relation' => 'AND',
// Only items from current product_cat
array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $currentTag->term_id,
'operator' => 'IN',
),
// AND are not featured
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
EDIT:
After some further research, it seems the problem is coming form another query further down the page. First, I perform 2 WP Queries as per the above, to retrieve different product types, and then merge these into a single array of ID's. Then, I perform the below wc_get_products() query to actually retrieve the product data. As you can see, the include arg states that only the ID's previously queried should be included, but it seems to ignore this when there are no matching products and instead revert to honour the per_page arg. Any way to avoid this and ensure they are excluded? Is there a way to 'exclude' anything that isn't in the $mergedQueries?
$productsData = wc_get_products(array(
'include' => $mergedQueries,
'paginate' => true,
'page' => $paged,
'limit' => 3,
'orderby' => 'post__in',
'return' => 'objects',
));

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

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
}

Wordpress taxonomy terms

So i'm having problem understanding, how can i set correct terms for my query. Right now my query is following:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $taxChild-> term_id,
),
)
);
Currently query is looping trough category taxonomy and returns every post that is queried via url. Example: http://website.com/category/{category-name}. Everything works correctly, except when i post new blog post then it shows latest category with the posts. Query queries all the categories as a sectors not post by post like it should be. Then i discovered that i can control the tax query with terms argument, but i'm not sure how can i set it up that i queries only most recent posts not categories.
Full source code for category.php https://pastebin.com/1Rvk1b7X
Example: https://imgur.com/a/abbI4

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 );

Meta Query Filter by Tag Wordpress

I currently have multiple pages, and in each page is a custom field which is named 'country'.
In the 'country' field I have this value 'uk, usa, brazil'.
What I'm wanting to do is display posts on the page which have the tags I have listed in the custom field 'country' (In this case show posts which has any of the tags 'uk', 'usa' and 'brazil').
I have the following code, but I don't know how to manipulate it to do the above?
$args = array (
'post_type' => array( 'post' ), // YOUR POST TYPE
'meta_query' => array(
array(
'key' => 'country',
'value' => $your_country, // THE COUNTRY TO SEARCH
'compare' => 'LIKE', // TO SEARCH THIS COUNTRY IN YOUR COMMA SEPERATED STRING
'type' => 'CHAR',
),
),
);
// The Query
$query = new WP_Query( $args );
It seems to just be filtering for a single value?
Any help to achieve the above would be greatly appreciated.
I'd setup a custom taxonomy to create another version of 'tags' called 'country', then you can do a taxonomy query instead.
See 'Custom Post Type UI' for an easy interface to create a custom taxonomy.
https://wordpress.org/plugins/custom-post-type-ui/
Install the CPT UI (plugin above).
Goto ‘Add / Edit Taxonomies’ in the menu. Add ‘Country’ as a taxonomy. Attach that to your ‘post’ post_type and 'page'.
Tag the pages (used to show the posts), and the 'posts' with your country tags.
You can then use the custom wp_query in a page template, to show the posts, tagged with the country tag.
To grab the ID's of the country terms, associated with your page:
$countryTerms = wp_get_post_terms($post->ID, 'country', array("fields" => "ids"));
Then change your Query arguments to gather the posts, tagged with the country.
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'country',
'field' => 'term_id',
'terms' => $countryTerms
),
),
);
If you want to stick to a meta field, try changing your 'TYPE' value to 'NUMERIC'.

Categories