Meta Query Filter by Tag Wordpress - php

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'.

Related

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

filter get_posts with ACF fields, nested

I have several post types:
Products
Reviews
Showcases
Manufacturers
Those are the two most important for this question
Reviews and Showcases are about a product, so when an user adds a review or showcase, they have to select the product trough an ACF relationship field.
When a user adds a product, they have to select the Manufacturer trough an ACF post object field
I have created all my custom pages for the reviews, product and showcases, now I arrived at the Manufacturer post type.
What I want here and am unable to achieve is show the latest 5 reviews, products and showcases with this manufacturer.
I know how to create a query etc, but have no idea what arguments to set in order to filter reviews and showcases (they work the same way, two levels nested) and products (one level nested) for that specific manufacturer.
Can somebody please post me in the right direction?
When you create an Query, you can ask for different post types in a query with givin an array of post types:
$args = array(
'post_type' => array( 'post', 'page', 'movie', 'book' )
);
$query = new WP_Query( $args );
In addition to that you can make a meta_query within this WP_Query in order to ask for the associated manufacturer:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => array('red', 'orange'),
'compare' => 'IN',
),
array(
'key' => 'featured',
'value' => '1',
'compare' => '=',
),
),
));
See the docs here.

Categories