Wordpress taxonomy terms - php

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

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

WP custom post type query does not return custom taxonomy

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

How to show posts containing only the selected taxonomy terms in Wordpress

I'm trying to add a taxonomy filter form to a page using a $_GET method to push the selected taxonomy terms to the tax_query. If more terms are selected then this should return only the posts containing those exact terms. Instead I get all posts that have at least 1 of the selected terms.
This is a single array so 'relation' => 'AND' won't work here. So instead I tried setting the 'operator' parameter to 'AND' but this gives me no results at all after selecting any of the terms. I even tried pushing a foreach loop inside the query to create a new tax_query array for each term hoping to get the relation parameter to do the work instead. But this didn't work and actually seemed like a bad idea.
This is my current query setup:
get_template_part('partials/blog', 'filter');
$paged = get_query_var( 'paged', 1 );
$blog_args = array(
'orderby' => 'date',
'paged' => $paged,
'posts_per_page' => 2
);
if(isset( $_GET['category'] ) ) {
$blog_args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $_GET['category'],
'operator' => 'AND'
),
);
}
$blog_query = new WP_Query($blog_args);
If a category is selected by the user that value get pushed to 'terms' as an array.
I expected the 'operator' parameter to act like the 'relation' parameter but this doesn't seem to be the case or am I doing something wrong?

Wordpress sort by number custom fields gives different results

I have a query which gives me custom post type posts which are sorted by category and a custom fields which has a number that indicates the amount of votes for the post. The problem is, if 2 posts have 3 votes, sometimes when you refresh, it changes their position. Here is the code:
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '=',
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'count_nominees'
);
I tried adding order on the category meta query, but nothing changes, I still sometimes get different results when refreshing, if the posts have same amount of votes. What is the right way to add second order, by ID or something?
Thanks.
As mentioned in the comments, I think this might help, but you might be able to extend it to be a little more specific in the search query for your use case.
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '='
)
),
'meta_key' => 'count_nominees',
'orderby' => array(
'count_nominees' => 'DESC',
'ID' => 'DESC'
)
);
That should get 10 posts in the nominees post type, only if they're part of category xyz, and have post meta of count_nominees and order by count_nominees first in descending order and then by the post ID in descending order.
You can use the WP_Query documentation on the WordPress codex for more information about complex queries using post meta data.

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

Categories