I am trying to get posts from the meta keys: sgt_slide and sgt_slide_home. The problem is that the function get_posts is resulting me an array with an object contained only from sgt_slide (post type is post), despite the fact sgt_slide_home (post type is attachment) meets the requirements for the query to be displayed.
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'sgt_slide',
'value' => 'on',
'compare' => '='
),
array(
'key' => 'sgt_slide_home',
'value' => 'on',
'compare' => '='
)
),
'no_found_rows' => true, //exclude unnecessary paging calculations
'numberposts' => -1,
);
$slides = get_posts($args);
var_dump($slides);
RESULT is
array (size=1)
0 =>
object(WP_Post)[309]
EXPECTED OUTPUT
array (size=1)
0 =>
object(WP_Post)[309]
1 =>
object(WP_Post)[309]
.
.
// should read also the posts with meta key sgt_slide_home
In the postsmeta table the posts from sgt_slide (post type = post) and posts from sgt_slide_home are visible, but it reads only the posts from sgt_slide
If you're querying posts and attachments, you'll have to include inherit in the post_status array:
'post_status' => array( 'publish', 'inherit' )
Related
I'm creating a one-way relationship between two products, with the 'linked-to' product outputting the product information of the 'linked-from' product.
I'm able to use a custom field to create the relationship, and use a meta query to generate the results based on this field, but I also need to check that the 'linked-from' product is still either In Stock (if not being stock managed), or has more than 0 as a stock quantity.
This is the approach I have taken so far, which returns no matching results (when indeed there are matches):
$the_query = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_key' => 'exact_new_version',
'meta_value' => $product->get_id(),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_stock_status',
'value' => array('instock'),
'compare' => 'IN',
),
array(
'key' => '_stock_quantity',
'value' => 0,
'compare' => '>'
)
)
));
The alternative is to check the stock of the returned match(es) in the query loop, but I imagine this to be far less efficient.
I've managed to achieve the desired result by passing in two sets of associative arrays into the meta query with an AND relationship, one of which (checking stock) containing a further two nested assoc. arrays with an OR relationship.
Essentially, the meta value has to match either an 'instock' item (if stock control isn't being used), or an item with '> 0' in stock, if it is:
$the_query = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' =>
array(
'relation' => 'AND',//*
array(
'key' => 'new_version',
'value' => $product->get_ID(),
'compare' => 'LIKE',
),
array(
'relation' => 'OR', //**
array(
'key' => '_stock_status',
'value' => array('instock'),
'compare' => 'IN',
),
array(
'key' => '_stock_quantity',
'value' => 0,
'compare' => '>',
),
),
),
));
I have to get posts having a particular meta value for a dynamic meta key.
The meta key values will be:
_project_global_1_trend_link
_project_global_2_trend_link
etc...
The common text in meta key is trend_link. So I need to add like operator for meta key.
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'projects',
'meta_query' => array(
array(
'key' => 'trend_link',
'value' => 10,
'compare' => 'LIKE'
)
)
));
By using this code I can apply like operator on meta_value.
But I need to apply like operator on meta_key.
Is there any way to apply like operator on meta_key.
Please help !!
For this situation you can use a parameter "compare_key"
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'projects',
'meta_query' => array(
array(
'key' => 'trend_link',
'compare_key' => 'LIKE',
'value' => 10,
'compare' => 'LIKE'
)
)
));
If i'm correct you can add a dollar sign to the meta key for dynamic keys!
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'projects',
'meta_query' => array(
array(
'key' => '_project_global_%_trend_link',
'value' => 10,
'compare' => 'LIKE'
)
)
));
I want to get posts of an specific post type where those have custom field with type of post type.
I have post type named "tour", and tour post type has custom field named "country" that its type is Post Object.
The query for get all tours that their country is "Turkey":
$located_tours = get_posts(array(
'numberposts' => -1,
'post_type' => 'tour',
'meta_query' => array(
'key' => 'country',
'title' => 'Turkey',
'compare' => 'LIKE'
)
));
But this query return all tours without any filtering on Country
Anyone have any idea?
Thanks
First of all, meta_query expects nested arrays, so it's not:
'meta_query' => array(
'key' => THE_KEY,
'value' => THE_VALUE,
),
but:
'meta_query' => array(
array(
'key' => THE_KEY,
'value' => THE_VALUE,
),
),
Note that compare isn't necessary in your case as its default value is =.
Anyway, you have a conceptual mistake here. If you are using ACF and your country custom field type is Post Object, the value for this field stored in the database is not title but the related post ID (either your Return Format is Post Object or Post ID).
Something like that (in your wp_postmetatable):
So, you need to query by country's ID, not country's Title. You could use get_page_by_title to retrieve your desired country ID:
// Assuming your Country's CPT slug is 'country'
$country = get_page_by_title( 'Turkey', 'OBJECT', 'country' );
$tours_args = array(
'posts_per_page' => -1,
'post_type' => 'tour',
'meta_query' => array(
array(
'key' => 'country',
'value' => $country->ID
)
)
);
$located_tours = get_posts( $tours_args );
Hope this helps!
Try this :
$tour_args = array(
'posts_per_page' => -1,
'post_type' => 'tour',
'meta_query' => array(
array(
'key' => 'country',
'value' => 'Turkey',
'compare' => '=',
),
),
);
$located_tours = new WP_Query($tour_args);
I will try and explain this a simple as possible.
I have 3 post types that come into play here.
reports
events (motogp-2013, motogp-2014)
circuits
On my 'events' single.php to display the event information, I am trying to create a query to display related 'reports'.
And the only relation that the event content can have with a report is the circuit.
When I create a 'report', I have to assign an 'event' to it. But when I create a 'event' prior to a report, I have to assign a 'circuit' to it.
For example, it looks like this...
> 'report' - Second place for Pedrosa as black flag terminates race for Marquez (1023)
> 'event' - Australian Grand Prix (662)
> 'circuit' - Phillip Island (156)
So I am trying to query 'reports', which had 'events' at specific 'circuits'.
On my 'events' single.php, I do have the circuit id, but I don't know how to list the reports that which are at that circuit.
I can easily show related 'events' using this query
$related = new WP_Query(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
But this is not what I'm trying to achieve, I need to show related reports.
This reports_race_event meta key contains the id number of the 'event', and the 'event' contains the meta key event_calendar_circuit which contains the circuit id.
My question is how do I do this for 'reports', when the only meta_key I have is reports_race_event
I need to list 'reports' which we're held at a specific $circuit
If any one can help me that would be great thanks.
I've figured out how I can do this!
But I still need help please, I can list all related reports like this...
$related_reports = new WP_Query(array(
'post_type' => 'reports',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'reports_race_event',
'value' => $events_at_circuit,
'compare' => not sure,
'type' => not sure
)
)
));
But I need to create an array of 'event' id numbers and store it in this $events_at_circuit variable.
Buy using this query first...
global $circuit;
$related_events = get_posts(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
My question is now, how do I get the returned post id numbers from the $related_events query and store them as an array in $events_at_circuit
I eventually got there...
Here is the answer, I had to store the array numbers for the events that where at that circuit, then queried by meta_key value using an array variable. Then comparing using wordpress meta IN comparison. See full code below...
$events_at_circuit = null;
$related_events = get_posts(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
if( !empty( $related_events ) ){ foreach( $related_events as $related_event ) $events_at_circuit[] = $related_event->ID; }
$related_reports = new WP_Query(array(
'post_type' => 'reports',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'reports_race_event',
'value' => $events_at_circuit,
'compare' => 'IN',
'type' => 'NUMERIC'
)
)
));
if ( $related_reports->have_posts() ) :
I am using Advanced Custom Fields plugin with WordPress. I am using relationship custom field.
$posts = get_posts(array(
'post_type' => 'slos_trainings',
'meta_query' => array(
array(
'key' => 'lokacija', // name of custom field
'value' => 79,
'compare' => 'LIKE'
)
)
));
I would like to pass array into meta_query value. Currently my code is working and it is returning all custom post types slos_trainings that have location custom meta field set to 79. But this field is a relationship, so it can have multiple ID-s. For example I want to find all posts with type slos_trainings that have lokacija set to 79 OR 200 OR 124, etc,..
How can I do this?
$args = array(
'post_type' => 'slos_trainings',
'meta_query' => array(
array(
'key' => 'lokacija',
'value' => array ( 79, 200, 124),
'compare' => 'IN'
)
)
);