Here is my problem:
For my articles, I use a custom post type “exhibitions” and posts for all other news. I am displaying them in one query [‘post_type’ = array(‘exhibitions’,’posts’)].
The orderby parameter for posts should be their publish date, for exhibitions it should be custom meta value (end date of the project). That way, posts would be displayed in following order:
- Exhibition 1 (publish date: 2.1. 2013, end date: 4.4. 2013)
- Post 1 (publish date: 13.3. 2013)
- Exhibition 2 (publish date: 1.4. 2012, end date: 1.3. 2013)
- Post 2 (publish date: 28.2. 2013)
Define PHP variable from date (posts) and end-date meta value (exhibitions) and then sort by it?
I came up with an idea to define PHP variable (end-date meta value for exhibitions, publish date for posts) and sort the wp_query by that.
Am I going in the right direction or not? If so, how to incorporate the variable to wp_query? I’ve found some related articles/questions, but none of them helped me so far.
Orderby filter?
Orderby filter sounds also as the way to go: Orderby filter @Wordpress.org forums
Thanks!
///
Johaness’s solutions is pretty much what I was aiming for. Here is my slightly modified function:
function save_sort_meta( $post_id ) {
$prev_val = get_post_meta( $post_id, 'sort_date', true );
/* set value only if not set yet (i.e. if this is a new post and not an update ) */
if ( 'exhibitions' === $_POST['post_type'] ) {
$my_meta = get_post_meta( $post_id, '_my_meta', true );
$datum_end_hidden = $my_meta['datum-end-hidden'];
update_post_meta( $post_id, 'sort_date', $datum_end_hidden);
} else {
/* using time() assuming "end_date" is a unix timestamp, adjust if not */
update_post_meta( $post_id, 'sort_date', time());
}
}
add_action( 'save_post', 'save_sort_meta' );
And here’s my query:
$args = array(
'showposts' => '22',
'post_type' => array('post','exhibitions'),
'post_status' => 'publish',
'meta_key' => 'sort_date',
'order' => 'DESC',
'orderby' => 'meta_value_num'
);
For some reason, “post” posts stay in the end of the wp_query (although when I echo custom value “sort_date”, it shows correctly and is larger than exhibitions, that should stay “bellow”).
Does it have some connection with my removal of checking the update of the post? I did it, because I want to change values on update as well.
Edit: Code above works!
Okay, I found out where was the problem – it’s quite embarassing. 🙂 The “end date” meta value of “exhibition” post types was provided by jQuery UI Datepicker – which saves the date in miliseconds.
All I needed to do was to divide $datum_end_hidden by 1000. 🙂
Thank you VERY MUCH!
To the best of my knowledge, you cannot sort a query by two different parameters without an additional iteration over the retrieved posts.
Since that ideally should be avoided, let me suggest a different approach:
How about incorporating an additional meta value “sort_date”, or the like? And then, in your saving routine, save either the end date of the exhibition or the publish date as “sort_date”.
Saving:
The Query:
As an aside, I’d recommend giving the post type a singular name.
I’m not sure if you mean you want to display 2 post types on the same page, both using a different sorting order.
If you are displaying exhibitions on their own archive, such as archive-exhibitions.php, you can set the query for that page to sort by the meta value you define.
Naturally, change
project_end_date
to reflect the key name of your project end date meta value. Changeorderby
tometa_value_num
depending on how you save the variable.meta_value
is probably fine.For the Loop, you’ll need to use:
Now, loading that archive (http://example.com/exhibitions/) will ignore the regular WP_Query and use your
my_own_query
for the loop.