I'm trying to get sorted list of posts using get_posts by meta value and the order of meta value is given in array.
This is what I currently have.
$stores = get_posts(array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // I only need the ID's of posts
'orderby' => 'meta_value',
'meta_key' => 'state',
'order' => 'ASC'
));
This returns the array of posts sorted by meta_value in ASCENDING alphabetical order.
I have an array of possible values for 'meta_key' => 'state', i.e. array('State1', 'State2', 'State3')
I want to set order so that all stores which has meta value State1 appears first, then from State2 and after that State3
I can't use order by numeric value and alphabetical value as state names are gonna be random.
I found one post here, it is using mera_query_orderby. I can't find any documentation for this and tried it, but it's not working. It returns posts ordered by ID.
Any help would be appreciated. Thanks
EDIT:
I added the meta_query_orderby filters in functions.php
And the updated code I used from EXAMPLE 2, is like:
$stores = get_posts(array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'state', // Custom field key.
'value' => array("CState1", "AState2", "BState3")
)
),
'meta_query_orderby' => array(
array(
'key' => 'state', // (required) Custom field key.
'value' => array("CState1", "AState2", "BState3")
)
)
));
I have an array of possible values for 'meta_key' => 'state', i.e.
array('State1', 'State2', 'State3')
If you want to sort the posts by the meta value in the exact order as in the above array, you can use a custom WP_Query parameter (to set the meta/sort values) and the posts_orderby filter to customize the ORDER BY clause, and in that clause, you would be using the FIELD() function in MySQL.
Step 1
Add this code to your plugin or theme (if theme, you'd add the code to the theme functions file):
add_filter( 'posts_orderby', 'posts_orderby_meta_value_list', 10, 2 );
function posts_orderby_meta_value_list( $orderby, $query ) {
$key = 'meta_value_list';
if ( $key === $query->get( 'orderby' ) &&
( $list = $query->get( $key ) ) ) {
global $wpdb;
$list = "'" . implode( wp_parse_list( $list ), "', '" ) . "'";
return "FIELD( $wpdb->postmeta.meta_value, $list )";
}
return $orderby;
}
Step 2
When making your post queries, set the orderby to meta_value_list and add meta_value_list to the query parameters — if you're using get_posts(), make sure suppress_filters is set to false:
$stores = get_posts( array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // get just the ID's of posts
'meta_key' => 'state',
'orderby' => 'meta_value_list',
'meta_value_list' => array( 'State1', 'State2', 'State3', '' ),
'suppress_filters' => false,
) );
PS: I the added '' to the array so that posts where the metadata is ('') (i.e. exists in the database, but the value is empty) would be placed at the bottom of the results.
Tried and tested working, but note that the above solution is only for single orderby, which means array is not supported.
/*Display posts of type 'stores', ordered by 'state', and filtered to show only states.*/
$args = array(
'post_type' => 'stores',
'meta_key' => 'state',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'state',
'value' => array( 'State1', 'State2', 'State3'),
'compare' => 'IN',
),
),
);
$query = new WP_Query( $args );
I am using Wp_Query method and I want to learn is there any parameter to limit my query.
I try to use LIMIT 1,2 in WP_Query.
Thanks for advance!
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'evet',
'compare' => '='
)
)
);
// The Query
$the_query = new WP_Query( $args );
you can use posts_per_page and offset args for WP_Query() like:-
$query = new WP_Query( array( 'posts_per_page' => 5, 'offset' => 3 ) );
for more :- http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
you are asking for LIMIT 1,2 (means offset,count(how many rows)) so
posts_per_page = count
offset = offset
I want to get more than one category with wordpress get posts function.
This is what I have:
$posts = get_posts(array(
'posts_per_page' => -1,
'category_name' => 'featured'
)
);
And I want to add the category "artworks". I tried like this but it doesn't work, any ideas how to do it?
$posts = get_posts(array(
'posts_per_page' => -1,
'category_name' => 'featured', 'artworks'
)
);
As noted in the Wordpress docs it looks like you have to pass category as a number, and I don't see 'category_name' as an option in the documentation.
It also appears that you can pass only a single category per get_posts() call.
So for a single category:
$posts = get_posts(array(
'posts_per_page' => -1,
'category' => '2'
)
);
Or for multiple
$vars = array(
array(
'posts_per_page' => -1,
'category' => '2'
),
array(
'posts_per_page' => -1,
'category' => '3'
)
);
foreach $vars as $post_array{
$posts[] = get_posts($post_array);
}
And print_r($posts); will have the resulting array.
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 );
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' );
}