Forums

[resolved] Sort list of posts (decending) by day of the week (12 posts)

  1. where_is_will
    Member
    Posted 1 year ago #

    Hi Guys,

    I am putting together a promotion website and using posts to create detailed events pages. On the home page I need to create the usual "coming events" list with a short intro and thumbnail.

    I need them to sort by day, not date, as the events all recur weekly. I am trying to use a custom field numbered 1 -> 7 to tell WP the order to sort in, and using $day=Date("l"); to grab the day of the week - but I can't work out how to compare them to sort the post list. I can display all the Monday posts, but not sort a list!

    I don't want to use an events plugin as they're not really flexible enough to display correcly. I'll keep working at it, but if anyone has any pointers that would be great.

    Thanks in advance, Will

  2. vtxyzzy
    Member
    Posted 1 year ago #

    If I understand the question correctly, I think you can use the 'meta_key=customfieldname' argument to you query and sort by 'orderby=meta_value'.

    See the Codex article here.

  3. where_is_will
    Member
    Posted 1 year ago #

    Thanks for the post - I understand what you are saying for the sort, thanks for the link.

    But it's a little trickier as I need to show the next event (post) depending on the day - so on Wednesday I need to show the events (posts) for Thursday, Friday etc. I can't use the date and sort on that as the same events recur weekly.

    I was hoping to create a value from Date("l") and use that to select the next relevant posts - but I can't work out the logic!

    Any ideas?

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Let me see if I understand. Do you want to select posts where the custom field value is greater than today's day number and order them by the custom field value?

    This may work for you:

    $today = date('w') + 1;   // Day of week with 1 for Sunday
    $tomorrow = ($today == 7) ? 1 : $today + 1;
    $args = array(
       'meta_key' => 'daynumber',
       'meta_compare' => ">=$tomorrow",
       'orderby' => 'meta_value'
    );
    query_posts($args);

    You may need to add in some more args to the query.

  5. where_is_will
    Member
    Posted 1 year ago #

    Ah, that's what I needed! Thanks for the quick help on it I'll try this and let you know how it works out.

    Will

  6. vtxyzzy
    Member
    Posted 1 year ago #

    That will only show posts where the day is greater than today. Is that what you want? Or, do you want it to wrap around so on day 4 it would show 4,5,6,7,1,2,3?

    That can be done, but it is a little different code. I'll try to test it out and get back to you.

  7. where_is_will
    Member
    Posted 1 year ago #

    Hi Vtxyzzy,

    Sorry, got side tracked into another project, only just picking this up again - thanks for the support. Yep, I'd like it to wrap so when we get to Tuesday it picks back up. Did you have a chance to have a play with that? If not I'll have a go with the work you started last time.

    Cheers,

    Will

  8. where_is_will
    Member
    Posted 1 year ago #

    For reference I had to change things slightly to get them to work, although this is probably more to do with the way the data is set up than anything else.

    <?php
    $today = date('w') + 1;   // Day of week with 1 for Sunday
    $tomorrow = ($today == 7) ? 1 : $today + 1;
    $args = array(
       'meta_key' => 'daynumber',
       'meta_compare' => '>=',
       'meta_value' => $tomorrow,
       'orderby' => 'meta_value_num',
       'order' => 'ASC',
       'posts_per_page' => 4
    
    );
    query_posts($args);
    
     ?>

    This works going forward - so on Wednesday it's showing Thurs etc, but when I move it forward to Friday I am not seeing the Monday events. The second line:

    $tomorrow = ($today == 7) ? 1 : $today + 1;

    must need an edit to allow it to wrap the events around - does anyone have any tips around that?

    Thanks All.

  9. where_is_will
    Member
    Posted 1 year ago #

    Having thought about this a bit more - this is not really an issue with the $tomorrow call at all, that's fine. What I need to do is put a loop into the post list (orderby field?) that says:

    If there are less than 4 posts in the list, restart the post list at a daynumber of 1.

    Any tips on how to loop a list of posts?

    Thanks All.

  10. vtxyzzy
    Member
    Posted 1 year ago #

    OK - here is how I think it will work, but only partially tested. You need to calculate a sortfield: if the daynumber is less than tomorrow, add 7 days, else use daynumber.

    <?php
    function mam_posts_fields ($fields) {
       global $mam_global_fields;
       if ($mam_global_fields) $fields .= (preg_match('/^(\s+)?,/',$mam_global_fields)) ? $mam_global_fields : ", $mam_global_fields";
       return $fields;
    }
    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_fields','mam_posts_fields');
    add_filter('posts_orderby','mam_posts_orderby');
    
    $today = date('w') + 1;   // Day of week with 1 for Sunday
    $tomorrow = ($today == 7) ? 1 : $today + 1;
    $mam_global_fields = ", if(meta_value < $tomorrow, meta_value + 7, meta_value) as sortfield ";
    $mam_global_orderby = " sortfield ASC";
    $args = array(
       'meta_key' => 'daynumber',
       'posts_per_page' => 4
    
    );
    query_posts($args);
    
    ?>
  11. where_is_will
    Member
    Posted 1 year ago #

    That's awesome thanks. All working like a dream, much appreciated.

  12. vtxyzzy
    Member
    Posted 1 year ago #

    Glad it works for you. Now, please use the dropdown at top right to mark this topic 'Resolved'.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags