My current code shows 5 posts from the cpt 'events'. This is the code i'm using:
//get post type ==> events
global $post;
$args = array(
'post_type' => 'events',
'numberposts' => $data['home_events_count'],
'orderby' => 'meta_value',
'meta_key' => 'timestamp_earth_event_start_date',
'meta_query' => $meta_query
);
I want to filter out posts (events) that their 'timestamp_earth_event_start_date' are older than the current date.
I've tried to modify the code myself, but still past events are shown.
Any help would be most appreciated!
Try this one passing query string
$numposts=$data['home_events_count'];
query_posts('post_type=events
&post_status=publish&numberposts=$numposts
&posts_per_page=1
&meta_key=timestamp_earth_event_start_date
&orderby=meta_value_num
&order=DESC')
By passing array
$args = array(
'post_status' => 'publish',
'post_type' = 'events' ,
'meta_key' => 'timestamp_earth_event_start_date',
'order' => 'DESC',
'orderby' => 'meta_value_num'
);
$event_posts = new WP_Query( $args );
Other option you can add filter for the query like
add_filter( 'posts_orderby', 'my_posts_orderby_date', 10, 2 );
function my_posts_orderby_date( $orderby, $query ) {
global $wpdb;
return " CAST( $wpdb->postmeta.meta_value AS DATE ) " . $query->get( 'order' );
}
Related
I am currently using WP All Export Pro and using WP_Query to pull only the last Woocommerce order completed. This is the code:
function example_get_those_ids($post_type = 'post', $number_of_posts = 5, $order = 'ASC', $post_status = 'publish', $order_by = 'ID') {
$query = get_posts(
array(
'post_type' => $post_type,
'numberposts' => $number_of_posts,
'order' => $order,
'orderby' => $order_by,
'post_status' => $post_status,
'fields' => 'ids'
)
);
return ( !empty($query) ) ? $query : array();
}
And then running this in the query.
'post_type' => 'shop_order', 'post_status' => 'wc_completed', 'post__in' => example_get_those_ids('shop_order', '1', 'DESC', 'wc_completed')
I was wondering how I can then go ahead and sort the products in that query by product description or a custom product field if possible.
Thank you.
I was searching for this but didn't find the solution. I am getting posts orderby title but some posts need to come first so I added a custom field which meta_key is display_postion and the values for this fields are numbers i.e. 1,2,3. So how do I achieve this, below is my current code.
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby'=> 'title',
'post_status' => 'publish',
'posts_per_page' => -1,
'paged' => $paged
;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts()) : $wp_query->the_post();
Adding meta_value_num to an orderby array and referencing 'display_position' in the meta_key field should do the trick:
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby' => array(
'meta_value_num' => 'ASC',
'title' => 'ASC'
),
'post_status' => 'publish',
'posts_per_page' => - 1,
'paged' => $paged,
'meta_key' => 'display_position'
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()):
while ($wp_query->have_posts()):
$wp_query->the_post();
See documentation for Order and Orderby parameters for WP_Query.
I am creating a website for a theatre company, and I am creating an index of all past, current, and future productions. I would like the index to 'orderby' the ending date of each production (ACF 'date' field type; 'ending_date').
Here is an example of my query:
<?php
$futureProd = array(
'post_type' => 'productions',
'posts_per_page' => -1,
'meta_key' => 'ending_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$slider_posts = new WP_Query($futureProd);
$array_rev = array_reverse($slider_posts->posts);
$slider_posts->posts = $array_rev;
?>
Have also tried the following, adding the 'meta_value_date' as well as 'meta_value_num' alternatives:
<?php // query posts
$futureProd = array(
'post_type' => 'productions',
'posts_per_page' => -1,
'meta_key' => 'ending_date',
'orderby' => 'meta_value_date',
'order' => 'ASC',
);
?>
AND
<?php // query posts
$futureProd = array(
'post_type' => 'productions',
'posts_per_page' => -1,
'meta_key' => 'ending_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
?>
No matter what I try, the posts refuse to order themselevs by the meta_value, and instead opt to order themselves by the default, post date.
I'm sure I'm missing something simple.
Anyone have any ideas?
You can spicify the datatype in orderby key like meta_value_*
possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME',
'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
Try this,
$futureProd = array(
'post_type' => 'productions',
'posts_per_page' => -1,
'meta_key' => 'ending_date',
'orderby' => 'meta_value_date',
'order' => 'ASC',
);
$slider_posts = new WP_Query($futureProd);
Please Note: for above code to work you need to have date in YYYY-MM-DD format if you have date in different format then you have to create your custom filter function hook in to posts_orderby filter with STR_TO_DATE MySQL function.
For date format YYYYMMDD
Add this in your active theme function.php file of your active child theme (or theme). Or also in any plugin php files.
function text_domain_posts_orderby($orderby, $query) {
//Only for custom orderby key
if ($query->get('orderby') != 'yyyymmdd_date_format')
return $orderby;
if (!( $order = $query->get('order') ))
$order = 'ASC';
global $wpdb;
$fieldName = $wpdb->postmeta . '.meta_value';
return "STR_TO_DATE(" . $fieldName . ", '%Y%m%d') " . $order;
}
add_filter('posts_orderby', 'text_domain_posts_orderby', 10, 2);
So now your WP_Query argument should look like this:
$futureProd = array(
'post_type' => 'productions',
'posts_per_page' => -1,
'meta_key' => 'ending_date',
'orderby' => 'yyyymmdd_date_format', //added custom orderby key
'order' => 'ASC',
);
The code is tested and fully functional.
Reference:
WP_Query Order & Orderby Parameters
MySQL ORDER BY Date field which is not in date format
Hope this helps.
I'm trying to retrieve the posts in Wordpress that have post_id < $number , so I looked for it in the documentation, and tried this code :
$args = array(
'numberposts' => 10,
'posts_per_page' => 10,
'offset' => 0,
'orderby' => 'id',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
array(
'key' => 'post_id',
'value' => '3000',
'compare' => '<'
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
but it didn't work (return the last 10 posts without comparing the ID), and I couldn't find an answer :(
So, could tell me please how to do it ..
And thanks in advance ..
Use the following query. it will return you array with your desired data.
global $wpdb;
$results = $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts WHERE ID < 10;" );
I have a custom post type called 'events' and I am to display the first 4 more recent events in event date order on the home page. I have got the events to show on the home page however the ordering doesn't seem to be working (see below the example). It seems to be ordering in its own way.
$args = array('post_type' => 'events', 'meta_key' => 'event-date', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => -1);
$events = new WP_Query( $args );
That is my code and here are the results (the dates) I get back.
16/04/2014
16/05/2014
19/03/2014
25/02/2014
27/02/2014
28/02/2014
As you can see, this is not ordering by ASC so what have I done wrong?!
Thanks in advance
Try to use meta_value_num instead of meta_value in orderby parameter. Use following code:
$args = array(
'post_type' => 'events',
'meta_key' => 'event-date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1
);
$events = new WP_Query( $args );