0

This is somewhat of a continued problem from another question that I had asked. Here is the original post. pre_get_post filter returns results when there should not be

Since then I added an orderby clause....

  if ($post_ids) {
                $wp_query->set('post__in', $post_ids);
                $wp_query->set('meta_key', '_start_date');
                $wp_query->set('orderby','meta_value');
                $wp_query->set('order', 'ASC');
            }else{
                $wp_query->set('post__in', array(0));

            }
            return $wp_query;

Problem is that I need to order by another meta_key's value. The meta_key is'_start_time'.

The $post_ids are compiled from another (complex)query and then I needed to compile that even more based on other factors. What I ended up with was the post ids that I needed and now I just need to display them in the order that I need. I have the order by _start_date working, but if there are more than one post on that date then they are not ordering by the right time that I need. So I also need to order based off the '_start_time' meta_key value, but can't seem to find a solution for this. Thanks for any help you can provide.

  • possible duplicate of Multiple orderby values in WP_Query – kaiser Dec 9 '13 at 13:59
  • The next time, please search the site before asking a question. Thanks. Also please document your efforts in code: Show us what you currently got, so we can work off of that. – kaiser Dec 9 '13 at 13:59
  • Thanks kaiser but I did and could not find a solution that fit my needs. Not sure how the link you provided works for me unless I am missing something. Can you provided and example? – David Labbe Dec 9 '13 at 14:17
  • Have you seen this: Wp_query order by multiple custom fields? – Nicolai Dec 9 '13 at 14:21
  • 2
    Do you have the possibility to make your starting time available in Unix Time? If so sorting should be pretty easy and doesn't require two fields. – Nicolai Dec 9 '13 at 14:28
0

I'm outlining a solution for what you asked in your last comment, because this wouldn't been fitting for the comment format.

Code:

function date_time_to_timestamp_meta( $post_id ) {

    // meta fields are saved in DATETIME, e.g. »2013-12-09 22:32:12«
    // here is only the date part relevant
    $datetime_date = get_post_meta( $post_id, '_start_date', true );
    // here is only the time part relevant
    $datetime_time = get_post_meta( $post_id, '_start_time', true );

    // we're extracting the relevant part with date()
    $extracted_date = date('Y-m-d', $datetime_date);
    $extracted_time = date('G:i:s', $datetime_time);

    // we're bringing back together the extracted relevant date and time
    $datetime_date_and_time = $extracted_date . ' ' . $extracted_time;

    // we're constructing the TIMESTAMP with strtotime()
    $date_time_timestamp = strtotime( $datetime_date_and_time );

    // we're updating/creating our TIMESTAMP meta field
    update_post_meta( $post_id, '_date_time_timestamp', $date_time_timestamp ); 

}   
add_action( 'save_post', 'date_time_to_timestamp_meta', 111 );

Note: exemplary and untested

As this is just a proof of concept above function for sure is missing some things you normally want to have, like conditional checks on post type and/or meta fields. Read into the according php functions, if you're not sure how to use them. I assumed a high priority for this, not knowing how and when the API calls you're talking about are happening, in short, you of course want to make sure this is happening after the two single date and time fields are available with the actual information you need.

  • Thanks for your help....I got it working now. Took some undoing of alot of things, but in the end it will be a better outcome. @ialocin thanks for the code sample...it is something that would not work with my situation, but I can use the concept from it like you said. I did the date compiling a little different, then the way you did, but I might try yours as mine is a little date functions overkill. – David Labbe Dec 10 '13 at 13:53
  • My pleasure, glad you could use a bit of it or have some inspiration for later use. Tick the answer, if you feel it's a near enough solution, I doubt you will get a more specific answer. @DavidLabbe – Nicolai Dec 10 '13 at 14:01
0

I'm not completely sure, but either it's white space separated, or ,/comma separated. One of those two options should work.

  • It's white space, but the real problem here is sorting against two meta keys, right? @DavidLabbe – Nicolai Dec 9 '13 at 14:02
  • Yeah, I'm pretty sure I've already answered that somewhere. Simply can't find it. – kaiser Dec 9 '13 at 14:09
  • I need to sort based off the meta_key's value – David Labbe Dec 9 '13 at 14:14
  • Normally I'm more doubtful, but of course you did. That said I can't find THE answer either. – Nicolai Dec 9 '13 at 14:19
  • @kaiser can you give me an example of how it is constructed. – David Labbe Dec 9 '13 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.