WordPress.org

Forums

Query for Sorting Post by Post Meta Fields (3 posts)

  1. Colin Murphy
    Member
    Posted 3 years ago #

    I have been stuck for a few hours on this problem and would love any suggestions on how to do this.

    I have post meta field that is a checkbox with the name and value "featured".

    I need to order posts that have been modified or created in the last 24 hours and also have the post meta field "featured" first followed by the rest of the posts.

    I have tried using the pre_get_posts filter but with no luck.

    Thanks in advance.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    Here is some sample code that I believe will do what you want:

    http://pastebin.com/p5mv7625

    Note that it sorts on the date published. If you want to sort on date modified, change this line:

    $mam_global_orderby = "sort_key, $wpdb->posts.post_date DESC";

    to this:

    $mam_global_orderby = "sort_key, $wpdb->posts.post_modified DESC";
  3. Colin Murphy
    Member
    Posted 3 years ago #

    Thanks very much. That worked perfectly :)

Topic Closed

This topic has been closed to new replies.

About this Topic