filter get_posts with ACF fields, nested - php

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.

Related

WordPress: Get posts from two custom fields in meta query

I'm using a Custom Post Type to store recipes.
Every recipe is related to certain products and categories.
In a product I want to display recipes related to it.
So first I want to show recipes which have the product ID in the custom field recipe_related_products and after that I want to show recipes which have the current product category in the custom field recipe_related_product_cats.
I'm using the ACF relationship field for that.
Here's my code:
$recipes = get_posts(array(
'post_type' => 'recipes',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'recipe_related_products',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE',
),
array(
'key' => 'recipe_related_product_cats',
'value' => '"' . $cat_id . '"',
'compare' => 'LIKE',
),
),
));
The code shows only recipes from the field recipe_related_products.
It seems that 'relation' => 'OR' has no effect.
If I remove the first array from the meta_query I get the recipes which are in the recipe_related_product_cats.
Is the 'relation' => 'OR' the problem?
It seems that booth meta queries are correct but they don't work together?!
To get the current ID from the product I'm using this code:
$cat_list = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'ids'));
$cat_id = (int)$cat_list[0];

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.

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

WP Query - Order by metavalue and keep other posts

Can't figure out this problem. Goal: I want to show all posts and order them by a post meta value, but not all posts have that meta key.
Problem: Not all posts are shown, only the ones that have that meta key (and meta value).
$args = array(
'post_type' => 'post',
'meta_key' => 'post_custom_field_1',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged,'post_type' => 'post' );
Question: How can I show all posts and sort them by a meta_key? Posts that have the meta_key are shown first and ordered by name and posts that don't have the meta_key follow and are sorted by title.
You'll have to use EXISTS for compare. There are plenty of examples in the docs for WP_Query, you can adapt it to your case:
function orderby_fieldifexists($orderby) {
return "mt1.post_id IS NOT NULL DESC, wp_postmeta.meta_value ASC";
}
add_filter("posts_orderby", "orderby_fieldifexists", 10, 1);
$query = new WP_Query(
array(
'post_type' => 'post',
'meta_query' => array(
"relation" => "or",
'custom_field_value' => array(
'key' => 'post_custom_field_1',
),
'custom_field' => array(
'key' => 'post_custom_field_1',
'compare' => 'NOT EXISTS',
),
),
'orderby' => array(
'custom_field' => 'ASC'
),
'posts_per_page' => 5,
)
);
I've initially had a simpler version in here, but then remembered that it would be silly to sort numerically if some values will be NULL. This version will find all posts, but get the value as well, and sort descending on "does it exist?" and then ascending on the actual values, so a post with a custom field value of -1 will be listed before those without that custom field value.

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