Multiples meta_key and order by specific meta_value - wordpress

I am trying to get the posts order by a meta_value that i put in a sortable list of posts, i have an extra meta_key and meta_value to give extra info to the query, but i can't make the WP_Query return the post order, do you have any idea that whats is worng?
This is my WP_QUery code:
$args = array(
'meta_query' => array(
array(
'key' => 'portada',
'value' => 'on'
), array(
'key' => 'pos',
'type' => 'numeric'
)
),
'meta_key' => 'pos',
'orderby' => 'meta_value_num');
$loop = new WP_Query($args);

i use the follow code to solve my problem:
$args = array(
'meta_query' => array(
array(
'meta_key' =>'destacados',
'meta_value' => 'on'
)
),
'orderby' => 'meta_value_num',
'category__in' => $category->cat_ID,
'meta_key' => 'pos'
);
$loop = new WP_Query($args);
Thank you all :)

Note that
'meta_query' => array(
array(
'key' =>'destacados',
'value' => 'on'
)
)
instead of
'meta_query' => array(
array(
'meta_key' =>'destacados',
'meta_value' => 'on'
)
)

Related

ACF - don't show passed events and filter with start date

I have a list of events and I'd like to do 2 things :
order posts according to starting date
don't include passed events in the query
I did some research and builded a query but it doesn't work.
'''
$current_date = date_i18n('d.m.y');
$the_query = new WP_Query( array(
'post_type' => 'spectacles',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'type' => 'DATE'
),
array(
'key' => 'header_spec_period_end',
'value' => '$current_date',
'compare' => '>',
'type' => 'DATE'
),
),
'order' => 'ASC' ) );
?>
'''
All the events are showing, I can't get the passed events filtered.
$date_now = date('Ymd');
$args = array(
'post_type' => 'spectacles',
'post_status' => 'publish',
'meta_key' => 'header_spec_period_start',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'compare' => '>',
'value' => $date_now,
),
),
);
$the_query = new WP_Query( $args );
You can try this ...

Wordpress - WP Query : meta_query relation ignored

I need help with a WP_Query. I use the meta query argument using the OR and AND relations, but this argument seems to be ignored in the result of the query.
Here is my code :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'author' => $contact_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $user_id )
),
array(
'relation' => 'AND',
array( 'author' => $user_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $contact_id )
)
)
);
$query = new \WP_Query( $args );
$response = $query->posts;
I already tried to add this argument like suggested in here:
'suppress_filters' => false,
'is_your_custom_query' => true, // your custom flag
Even if I remplace the value of $user_id and $contact_id directly in the query with any number, the query still return the same result. I don't understand why it's not working.
Thank you for your help !
As suggested by dafoxuk, I have to remplace meta_key by key and meta_value by value. I also had to add 'compare' => 'LIKE'.
But event in this case it wasn't working. I also had to stock the author_id in a post_meta, and change the array( 'author'=> $contact_id ) condition by :
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
So the final $args array look like this :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $user_id, 'compare' => 'LIKE '
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $contact_id, 'compare' => 'LIKE' )
)
);

Wordpress advanced custom field query giving wrong results

I have the following code. It is supposed to return only the post from 'portfolio' custom post type where the format is 'print', the checkbox for 'welcome_gallery' is checked and the 'market' value is 'italy'. But it returns also other post not matching these key values.
$recent_posts = array(
'post_type' => 'portfolio',
'numberposts' => 8,
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'format',
'meta_value' => '%print%',
'compare' => 'LIKE'
),
array(
'meta_key' => 'welcome_gallery',
'meta_value' => '1',
'compare' => '='
),
array(
'meta_key' => 'market',
'meta_value' => '%italy%',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $recent_posts );
I've tried hard to make it work but it doesn't. Before I get crazy, is there someone who can help me to undestand the reason?
Thanks in advance.

How to use custom fields in wp_query for ordering and filtering the result at the same time?

I want to query the posts that have been set as "featured" and order them by their "priority" field at the same time.
featured field is a true/false type and priority field is a number, and they are created by ACF plugin.
here is my code, but it's not working...
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num date',
'meta_key' => 'priority',
'meta_query' => array(
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
try this :
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num',
'order'=>'DESC',
'meta_key' => 'priority',
'meta_query' => array(
relation=>'AND',
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
array(
'key' => 'priority',
'value' => array(1,6), //YOUR VALUES
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
),
);
$query = new WP_Query( $args );

How to sort multiple wordpress custom field values?

Display posts with 'Product' type ordered by 'Price' custom field:
$query = new WP_Query(
array ( 'post_type' => 'product',
'orderby' => 'meta_value',
'meta_key' => 'price' )
);
Which code should I use if also want to order by 'Size'?
Another example on which I need multiple sort on custom fields:
Display posts with 'Event' type ordered by 'Start_Hour' and then by 'Start_Minute'.
Thanks to Bainternet I found the solution:
function orderbyreplace($orderby) {
return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
}
and...
$args = array(
'post_type'=>'Events',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'Start_Hour',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'Start_Minute',
'value' => '',
'compare' => 'LIKE'
)
)
);
add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
I think this changed slightly in Wordpress 3.7.
I had to change
return str_replace('menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
into
return str_replace('wp_posts.menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
Thanks for the initial solution! [Edited]
Here's an example of using more than one meta_key and orderby that I believe should work:
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'price',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'size',
'value' => '',
'compare' => 'LIKE'
)
),
'orderby' => 'price size',
'order' => 'ASC'
);
$query = new WP_Query;
$resulting_obj = $query->query($params);
You'll need to play with the meta_query items a bit more, especially the 'value' parameter. Please have a good look at http://codex.wordpress.org/Class_Reference/WP_Query in the 'Custom Field Parameters' section.
You don't need any filter or hooks to sort multiple custom fields, if your one of custom field is meta_key and other one is normal column of table, than use these parameters/arguments in your query.
'meta_key' => 'KEY_NAME',
'orderby' => 'meta_value_num SECOND_COLUMN_NAME',
'order' => 'ASC' or 'order' => 'DESC'
Here the order of meta_value_num and SECOND_COLUMN_NAME matter, you may see different-2 result based on the order.

Resources