Wordpress meta_query how to use "like" comparison on meta_key - php

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

Related

Wordpress Query with Multiple meta_keys not working

Ok, so I am querying events made by The Events Calendar, using Advanced Custom Fields. I have a plugin that converts the serialized data to standard under a new meta_key. That said, I am able to query events by the meta_key and meta_value individually. i.e
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_key' => 'display_override',
'meta_value' => 'Arkansas Literary Festival'
);
and also
// args
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_key' => 'display_global',
'meta_value' => 'Enabled'
);
However, I can not get them to work simultaneously, i.e
// args
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'display_override',
'value' => 'Arkansas Literary Festival',
'compare' => '='
),
array(
'key' => 'display_global',
'value' => 'Enabled',
'compare' => '='
)
)
);
When I experiment with this, by using 'OR' or 'AND', and 'LIKE' instead of '=', I either get no posts, or I get the master list of unfiltered posts....

How to order WP_Query by meta key value but first remove letters?

I'm trying to sort my WP_Query by a meta key but the problem is that the meta key is a string and not a number. What i want to do is somehow remove the letters from the string and then order by this value.
For example, 'meta_key' contains letters before the number like FOR04. Is it possible to somehow remove the letters from this within the $args?
Can i do something like this?
'meta_key' => preg_replace('/[^0-9]+/', 'test_2')
$args = array(
'post_type' => 'property',
'meta_query' => array(
array(
'key' => 'test_1',
'value' => $post->ID,
'compare' => 'IN',
),
),
'meta_key' => 'test_2',
'orderby' => 'meta_value_num',
'posts_per_page' => -1
);
I've tried this method but it's not sorting them correctly..
$args['meta_key'] = preg_replace('/[^0-9]+/', '', 'db-id');
$args = array(
'post_type' => 'property',
'meta_query' => array(
array(
'key' => 'court',
'value' => $post->ID,
'compare' => 'IN',
),
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => -1
);

Wordpress - Multiple Meta Keys - Order by rank

Hello I have something like this
if(isset($_POST["select_1"]) or isset($_POST["select_2"])){
$args = array(
'numberposts' => -1,
'post_type' => 'my_post_type',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $meta_key_list1,
'value' => $meta_key_val,
'compare' => '='
),
array(
'key' => $meta_key_list2,
'value' => $meta_key_val,
'compare' => '='
),
array(
'key' => $meta_key_list3,
'value' => $meta_key_val,
'compare' => '='
),
),
);
}else{
$args = array(
'numberposts' => -1,
'post_type' => 'my_post_type',
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'page_rank'
);
}
I need show all pages which have set custom field "page_rank" and order it DESC. "Else" working good but first part not working. I try something like in "else" but this not working for multiple key.
Please do you know anyone how to solve this problem?
Thank you !
EDIT:
Solved I changed OR to AND thank you for help
Try 'compare' => 'LIKE' instead of 'compare' => '=' and verify your keys and values in the meta query

meta_query not working in wp_query

My post type is product. I use a checkbox field with meta key is ht_featured, meta value when I print_r is array([0] => featured).
My WP_Query:
$the_query = new WP_Query(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
);
It doesn't show any post.
I tried with value => 'featured' and 'compare' => 'EXISTS' but it not working.
WP_query needs to be passed in an array. use following code and let me know if any prob.
$the_query = new WP_Query (array (
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
You can refer to the discussion at wordpress forum:
http://wordpress.org/support/topic/how-to-wp_query-meta_query-value-string-contain-in-key-string
You're passing all of this into WP_Query as individual arguments when they should be contained in an array.
$the_query = new WP_Query( array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN',
),
),
) );
Can you clarify your point about the checkbox? I'd suggest simply updating 'ht_featured' with either 'yes' or 'no' when you save the product. Then change your 'value' in the meta query to 'yes' and remove the 'compare'.
Are you sure there is no php error?
I think WP_Query needs to be passed in an Array
$the_query = new WP_Query(
array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
I had a similar problem until I used the function get_posts() rather than creating a new WP_Query. See if that helps...

php query based on relational child elements for wordpress loop

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() ) :

Categories