WordPress: Get posts from two custom fields in meta query - php

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

Related

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.

How can I separate the output of a wp_query into separate sections

I have a WP Query that is checking a custom post types metafields.
I have two custom post types:
Country
Media
The country custom posts type is simply created to pull the content of the media post type into.
The Media CPT contains 2 metafields:
The first metafield is a media filter selection that allows the selection of "TV", "Radio", "Digital" and has this ID _rtl_media_filter.
The second contains a country to associate the post type to and has this ID _rtl_country_filter, which is pulling the country CPT name into a dropdown and can be selected from within the media post type.
The aim is to display all the media types that are assiciated with the country CPT when the country CPT is viewed.
I have this loop, which works. But am wondering if I can group a single loops item by media type _rtl_media_filter? Rather than creating multiple loops for each?
<?php
// Grab this posts ID
$post = $wp_query->post;
$this_id = $post->ID;
// Query the meta vaules against the posts ID
$args = array(
'post_type' => 'media',
'meta_query' => array(
'relation' => 'OR',
array( // TV
'relation' => 'AND',
array(
'key' => '_rtl_media_filter',
'value' => 'tv',
'compare' => '=',
),
array(
'key' => '_rtl_country_filter',
'value' => $this_id,
'compare' => '=',
),
),
array( // DIGITAL
'relation' => 'AND',
array(
'key' => '_rtl_media_filter',
'value' => 'digital',
'compare' => '=',
),
array(
'key' => '_rtl_country_filter',
'value' => $this_id,
'compare' => '=',
),
),
array( // RADIO
'relation' => 'AND',
array(
'key' => '_rtl_media_filter',
'value' => 'radio',
'compare' => '=',
),
array(
'key' => '_rtl_country_filter',
'value' => $this_id,
'compare' => '=',
),
),
),
'posts_per_page' => -1,
);
$the_query = new WP_Query( $args );
if($the_query -> have_posts()) :
while ($the_query -> have_posts()) : $the_query -> the_post();
echo '<h2>' . the_title() . '</h2>';
endwhile;
wp_reset_postdata();
endif;
?>
This is how I would like the output:
|TV|
ITV | ITV2 | ITV3
|RADIO|
Radio 1 | Radio2
|DIGITAL|
HUB 1 | HUB 2
So After Antonis pointed out about ordering by meta_value I figured out the way it can be done. For anyone else trying this the solution lies with these couple of lines:
'orderby' => 'meta_value',
'meta_key' => '_rtl_media_filter',
Add those to the bottom of your loop query.

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

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.

Wordpress meta_query Not Working with 'orderby' - Advanced Custom Fields

I'm trying to query a custom post type 'section' where the 'section.section_parent_page = {whatever the ID of the current $post is}'.
This part of the query works fine and is accomplished like this:
$args = array(
'post_type' => 'section',
'meta_query' => array(
array(
'key' => 'section_parent_page', // name of custom relate field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);
But once I try to 'orderby' the custom field 'section_position', I either break the query or see no proper ordering. Here's what I currently have:
$args = array(
'post_type' => 'section',
'meta_key' => 'section_position', // custom field I want to order by
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'section_parent_page', // name of custom relate field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);
When I look at the $the_query->request, it looks like it continues to order by wp_posts.menu_order
How do I use multiple meta_query arrays to accomplish this?
EDIT:
No matter what I try, the end of my query string ends up looking like this:
ORDER BY wp_posts.menu_order ASC LIMIT 0, 10
If section_position has integer ( number ) value, use following code
$args = array('post_type' => 'section',
'orderby' => 'meta_value_num',
'meta_key' => 'section_position',
'order'=>'ASC',
'meta_query' => array(
array(
'key' => 'section_parent_page', // name of custom relate field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);
Note i have used "meta_value_num" instead of "meta_value"
I attempted to do the same thing recently within an ACF filter. I contacted ACF support and received this answer.
Unfortunately i this query is too complex to be passed through WP_Query and you would have to do a manual SQL query to achieve this.
The problem is you are doing two meta_querys, the meta_key = start date and the ones inside the brackets are conflicting.
You need the flexibility of full SQL syntax to perform a query like that.

Categories