I'm struggling to get a working solution with this wp_query. I currently have some custom settings which are assigned to posts, one is whether or not the post is 'featured' and the second is a date and time for the post to end (no longer display in the results). I have the query working with the feature, but just need to work this end date into it, here is the query working find with the 'featured':
WP_Query('meta_key=skyali_feature&showposts=4&orderby=post_date');
The end date is set in the wp_postmeta table where meta_key is 'the_date' and the meta_values look like this '05/16/2013 05:24'. I would like to edit the above query where if 'the_date' has been set posts are only included if the 'the_date' is greater that todays date and time.
Here is my failed attempt:
WP_Query(
'meta_key=skyali_feature&meta_key=the_date&meta_compare=>=&meta_value='
.date('d/m/Y H:i')
.'&showposts=4&orderby=post_date&orderby=the_date'
);
I had to do something very similar recently and ended up needing to use the meta_query property instead. You'll want to do something like this:
$today = date('Ymd');
$args = array(
'post_type' => 'post',
'posts_per_page' => '4',
'meta_key' => 'the_date',
'meta_query' => array(
array(
'key' => 'skyali_feature'
),
array(
'key' => 'the_date',
'value' => $today,
'compare' => '>='
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
A few notes...
I only needed to filter by date in my example, but it looks like you'll need to do date/time in yours. (You can just adjust the first line for the $today variable using the format you wish).
Use posts_per_page instead of showposts. showposts is deprecated.
Notice that I have included the meta_key twice (once at the top level of the array and once as an element in the meta_query array. There's a known bug where you can't sort your results by the key if you don't include it this way. I fought that one for a while too!
Hope this helps, have fun!
[edit] Forgot to add your skyali_feature key back into the array.
for people using the advanced custom field plugin with a date data type, this is what you need for dates greater or equal than today:
$today = date('Ymd');
$args = array(
'post_type' => 'post',
'meta_key' => 'end-date',
'meta_query' => array(
array(
'key' => 'end-date'
),
array(
'key' => 'end-date',
'value' => $today,
'compare' => '>='
)
),
'orderby' => 'meta_value',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
for people using Custom metadata manager you'll find that a datepicker field is stored as timestamp.
So in a similar case the above example isn't working and you may have php sort out the value you need to compare against. And the timestamp for a day earlier at 23:59:59 it'll do the job:
$yesterday = strtotime('yesterday 23:59:59');
$args = array(
'post_type' => 'post',
'meta_key' => 'end-date',
'meta_query' => array(
array(
'key' => 'end-date'
),
array(
'key' => 'end-date',
'value' => $yesterday,
'compare' => '>='
)
),
'orderby' => 'meta_value',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
If you also want to take account of the timezone setting for the blog use current_time() as in the following example:
$now = current_time('timestamp');
$yesterday = mktime(23, 59, 59, date('n',$now), date('j',$now)-1);
Related
I have set on my WordPress post custom meta called "my_sort" which have a numeric values like 1, 2, 3, 4..... See below image:
I want to sort in ascending order by this meta "my_sort". This is the below code I am applying:
<?php
$catPost = get_posts(array('category' => get_cat_ID($categories[0]->name), 'meta_key' => 'my_sort', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'numberposts' => 100)); //change this
?>
The problem with this code is that it is working but has one problem which I need to be fixed.
It is leaving all the other posts which do not have "my_sort" meta in them. I want to include those posts also. I want:
First the posts that have "my_sort" must come in ascending order.
Then the post that do not have "my_sort" meta should come.
Update
When I try the below query.
$catPost = get_posts(
array (
'category' => get_cat_ID($categories[0]->name),
'numberposts' => 100,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'my_sort',
'compare' => 'EXISTS'
),
array(
'key'=>'my_sort',
'compare' => 'NOT EXISTS'
)
),
)
);
I do not get proper result. See the image which shows the result.
Here:
Record1 : does not have "my_sort" meta.
Record2 : does have "my_sort" and it's value is 2.
Record3 : does not have "my_sort" meta.
Record4 : does have "my_sort" meta and it's value is 4.
The result in this case should be like this:
Record2 then Record4 then Record1 then Record4 but clearly this is not the case.
What is wrong?
If you want to sort by a meta key that might not have any values, you need to use a meta_query that combines the results of a search for posts with the key and a search for posts without the key.
This isn't tested, but the main logic is there:
$catPost = get_posts(
array (
'category' => get_cat_ID($categories[0]->name),
'numberposts' => 100,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'my_sort',
'compare' => 'EXISTS'
),
array(
'key'=>'my_sort',
'compare' => 'NOT EXISTS'
)
),
)
);
I'm using this query do get some posts.
$args = array(
'post_type' => 'spaces',
'post_per_page' => '500',
'orderby' => 'rand',
'meta_key' => 'space-city',
'meta_value' => $search,
);
$query = new WP_query($args);
Now I need to order the results by total of comments on each post. I've a custom field with this number called "space-comments", but I've no idea how to sort this posts with this second meta_key.
I made some tests, but I only was able to get post when "space-comments" has a value. When there is no value, the post don't show up.
Any ideia how can I start?
The WP_Query can accept a meta_query argument that is populated as an array of sub-arguments. That array can have sub arrays that are each their own meta query, so you can create some nice compound searches across meta data.
See the example from https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters below.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
I have a template that pulls a custom post type for properties and sorts them by state. The script works properly until you add in a meta_query for the agent. It pulls the correct properties, but it doesn't sort them by state with the meta_query present. Here's the query:
$qry = array(
'post_type' => array( 'practices-tpsg' ),
'meta_key' => 'wpcf-practice-state',
'orderby' => 'meta_value',
'order' => 'asc',
'showposts' => 18,
'paged' =>$paged
);
if($_GET['agent'])
{
$qry['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'wpcf-agent',
'value' => $_GET['agent']
),
array(
'key' => 'wpcf-agent2',
'value' => $_GET['agent']
),
);
}
Does anyone know why it would stop sorting properly after the meta_query is added to the query?
Here's a link to the page: http://www.totalpracticesolutionsgroup.com/practices-for-sale/ The sort order works properly in the default view, which allows the state name headers above the listings. If you select an agent from the drop down in the top right, the correct properties are pulled, but they are no longer sorted by state. You can also see the output of the query by adding &debug=1 to the end of the url.
replace 'orderby' => 'meta_value' with 'orderby' => 'meta_value_num
i have one event plug-in and now i want to show my event post like current events,upcoming events then past events.i have created one custom post for this and create 2 meta-boxes for start date and end date. But now how i call?
Without more details, I just can say you'll probably need to perform a query similar to this one:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => 'end_date',
'meta_query' => array(
array(
'key' => 'endt_date',
'value' => time(),
'type' => 'numeric',
'compare' => '<'
)
)
);
$past_events = new WP_Query( $args );
This would return the past events, assuming your post type is called events, you have a custom field end_date that stores a numeric timestamp...
You should modify the meta_query accordingly for current and past events, using different value and compare arguments.
Take a look at docs for WP_Query class and in particular to the Custom Field Parameters section.
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.