Wordpress query by multiple metaboxes and order by date - php

So here is my query:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'meta_key' => 'event_informations_show_on_the_homepage',
'meta_value' => 'Show on the homepage',
'meta_compare' => '==',
'meta_key' => 'event_informations_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
I want to select all posts that have the metabox event_informations_show_on_the_homepage and the value of the metabox event_informations_show_on_the_homepage and order by the date metabox which is stored as a timestamp and is called event_informations_date.
What am I doing wrong?

Hopefully I'm not barking up the wrong tree here.
You can use the key 'meta_query' to filter posts by multiple meta keys like so:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_informations_show_on_the_homepage',
'value' => 'yes',
),
array(
'key' => 'event_informations_date',
'value' => 'yes',
)
)
);
$query = new WP_Query( $args );
What WordPress is doing here is creating multiple wheres against the same column by using innerjoins on the same table, each time using a different alias. It's pretty cool & is probably the fastest way to query like that.
For more information see here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Hope this helps :)

Related

How to query posts by ACF field but then also order by a separate ACF field?

I am using WP_Query to get posts that have a specific value in one of the ACF fields. I also need to order them by a separate ACF field. I am not sure how to accomplish this. Everything I've read says to use 'orderby' => 'meta_value' but I believe thats the value of the field I'm filtering the posts by, which is not what I want.
This is what I have right now..
$args = array(
'post_type' => 'contacts',
'posts_per_page' => -1,
'meta_key' => 'department',
'meta_value' => 'Transportation',
'orderby' => 'meta_value'
);
$the_query = new WP_Query( $args );
I need to orderby an ACF field named last_name.
It's possible to assign a name to a meta query, and then refer to that name in your orderby. Something like this.
$args = array (
'post_type' => 'contacts',
'post_status' => 'publish',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array( 'main_query' => array(
'key' => 'department',
'value' => 'Transportation'
), 'orderby_query' => array(
'key' => 'last_name',
)
),
'orderby' => array(
'orderby_query' => 'ASC',
),
);
$the_query = new WP_Query( $args );

WP_Query filtering

So I've been looking around here and other sites for a solution. I found lots of really helpfull posts but for some reason I just cant get this to work.
What I have:
WP posts with custom fields.
One is "rating" which is given a number between 1-5
The other is "flash" with either a 1 or a 0.
What I want to do:
Show all posts with a 1 on flash, in ORDER descending by the "rating"...
I currently have:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'meta_key' => 'flash',
'meta_value' => '1',
)
);
$ultimos = new WP_Query( $args );
This does NOT filter the flash custom field.
however if I do:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'flash',
'meta_value' => '1',
);
$ultimos = new WP_Query( $args );
This DOES filter flash, but does not order them properly.
Any thoughts?
I believe you need to take a look at using the relationship feature of the WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'flash',
'value' => '1',
'compare' => 'LIKE',
),
);
$ultimos = new WP_Query( $args );

Order by multiple custom field values in wordpress

I have a custom field called "sale_status" and the values ("For Sale", "Sold", "Let") are displayed in a radio button in admin. Now each of them can be assigned to a single property.
Currently the query fetches the properties order by post date but I want it to be by sale_status and then date.
My code is as below--
$args = array(
'post_type' => 'zoo-property',
'posts_per_page' => $query__per_page,
'post_status' => 'publish',
'paged' => $query__page,
'meta_key' => 'sale_status',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
$query__types,
$query__locations,
$query__statuses,
$query__investments,
$query__price
)
);
$properties_wp_query = new WP_Query($args);
echo "Last SQL-Query: {$properties_wp_query->request}";
But it not showing in correct order.
Any help is highly appreciated. Thanks in advance.
When you want to include multiple sort criterion in the same query the orderby value must be an array. See WP_Query documentation for details. Try something like this:
$args = array(
'post_type' => 'zoo-property',
'posts_per_page' => $query__per_page,
'post_status' => 'publish',
'paged' => $query__page,
'meta_key' => 'sale_status',
'orderby' => array( 'meta_value_num' => 'DESC', 'post_date' => 'DESC' ),
'meta_query' => array(
'relation' => 'AND',
$query__types,
$query__locations,
$query__statuses,
$query__investments,
$query__price
)
);

How to combine meta_query and orderby date meta in WordPress?

I have 3 meta fields, date (date formatted string), location, and consultant.
I can pull the posts and orderby the date field when meta_key is set:
// get posts
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
However, I have not be able to get this to work with a more complex meta_query:
$args = array(
'post_type' => 'uppy_events',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => $key,
'value' => array($value),
'compare' => '>='
)
),
'numberposts' => -1
);
Basically I want to pull all posts that have a meta start_date greater than today's date and order by these dates.
As a side note - can you do a meta_query search on say location key and still orderby date key?
This is the first time I've ever tried ordering by meta, and have found some good posts but still cannot wrap my head around it.
OK, I figured this out as my post lead me to another good example. The commented out meta_query is date greater than today. The other is using a different key. Both work!
$today = date("Ymd");
$args = array(
'post_type' => 'uppy_events',
'post_status' => 'publish',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
/* array(
'key' => 'event_date',
'value' => $today,
'compare' => '>='
) */
array(
'key' => 'event_location',
'value' => 'vancouver',
'compare' => '='
)
),
'numberposts' => -1
);

Ordering the output of a loop by a custom field containing a datestamp

I'm using a custom field that stores the timestamp of a date entered by the user. I want to display posts sorted by that custom date, but they refuse to order by custom field.
Here I register the custom field using this custom meta boxes tool - https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
// the prefix is _cmb_
array(
'name' => 'Start Time',
'id' => $prefix . 'start_time',
'type' => 'text_datetime_timestamp',
),
Then I use WP_Query to loop through the posts with the following arguments:
$args = array(
'post_type' => 'talks',
'posts_per_page' => -1,
'orderby' => '_cmb_start_time',
'order' => 'ASC'
); // show talks ordered by start time
$custom_query = new WP_Query($args);
This prints out the posts in the order they were added and not in the order of the custom field date.
Update
I was also trying to filter by another custom field type "room" with a value equal to "room 1":
$args = array(
'post_type' => 'talks',
'posts_per_page' => -1,
'orderby' => '_cmb_start_time',
'order' => 'ASC',
'meta_key' => 'room',
'meta_value' => 'room 1'
);
Solution:
With #joebuckle 's solution I ended up with this version that works great:
$args = array(
'post_type' => 'talks',
'posts_per_page' => -1,
'meta_key' => '_cmb_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'room',
'value' => 'room 1',
'compare' => '='
)
));
try it like this instead (ref WP_Query)
$args = array(
'post_type' => 'talks',
'posts_per_page' => -1,
'meta_key' => '_cmb_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC'
); // show talks ordered by start time
$custom_query = new WP_Query($args);

Categories