Forums

[resolved] Sorting by priority (Custom Fields) Pt. 2 (9 posts)

  1. Daffydd57
    Member
    Posted 2 years ago #

    Hi all!

    I'm currently using this:

    query_posts('meta_key=priority&orderby=meta_value&order=ASC');

    To sort posts - based on a number inserted in the custom field value. Small problem is that I am restricted to 1-9 as it does not recognize the second digit - ex: "10" is seen as "1".

    is there a way to open that query up to allow for double digits?

    Thanks!

  2. vtxyzzy
    Member
    Posted 2 years ago #

    There may be a simpler way, but I am certain that it can be done using the technique shown in this code.

    You will need to register three filters:

    add_filter('posts_fields','mam_posts_fields');
    add_filter('posts_join','mam_posts_join');
    add_filter('posts_orderby','mam_posts_orderby');
    $paged = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;

    Then, set up the values for the filters to add to the query:

    $mam_global_fields = 'intval(wpmeta.meta_value) as sortval';
    $mam_global_join = "LEFT JOIN $wpdb->postmeta wpmeta ON
             ({$wpdb->posts}.ID = wpmeta.post_id AND wpmeta.meta_key = 'priority')";
    $mam_global_orderby = 'sortval';
    query_posts("paged=$paged")

    And, either in functions.php or at the end of your template, define the filters:

    <?php function mam_posts_fields ($fields) {
    global $mam_global_fields;
    return "$fields, $mam_global_fields";
    }
    function mam_posts_join ($join) {
    global $mam_global_join;
    return "$join $mam_global_join";
    }
    function mam_posts_orderby ($orderby) {
    global $mam_global_orderby;
    return "$mam_global_orderby, $orderby";
    }?>
  3. Daffydd57
    Member
    Posted 2 years ago #

    Hi vtxyzzy,

    Thank you for that. But it's way over my head and it appears to be doing much more than I need. I already have the orderby set up - I just need it to recognize double digits. Is that what all of that above is doing?

    thanks again

  4. ambrosite
    Member
    Posted 2 years ago #

    If WordPress is storing those custom field values as strings (which is probably is), then '10' would come before '2' in a sort. What I would do is start numbering at '101' instead of '1'. That way, the numbers will sort in the correct sequence.

  5. Daffydd57
    Member
    Posted 2 years ago #

    Hi ambrosite,

    That did not appear to make any difference actually.

    I took a story I wanted to be first and used "101" - it did show up first. I then changed the priority to "110" (to simulate "10") and it still remained on top of the other stories with smaller priority numbers. Did I misunderstand your suggestion?

    Thanks!

  6. Daffydd57
    Member
    Posted 2 years ago #

    Scratch that - I think it's working after all - I went back in and changed all the priorities to use the 3 digits and it now appears to be working. Thanks!!!

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

    Pad your numbers with zeros and it will work just the same..

    eg..

    001
    002
    011
    100

    ..etc..

  8. Daffydd57
    Member
    Posted 2 years ago #

    Cool - thanks! That seems to be working fine. :)

  9. benz1
    Member
    Posted 1 year ago #

    Apologies for jumping in on this old thread but I'm trying to do the same thing as the OP, i.e., sort posts according to a priority set as a custom field, but where exactly to you put the query, "query_posts('meta_key=priority&orderby=meta_value&order=ASC');" to sort the posts wherever they appear, e.g., home page, archive page, recent posts widget, etc?

    There's an old plugin http://wordpress.org/extend/plugins/wp-smart-sort/ that let you sort by custom field but it doesn't work in the current version of WP, and another plugin http://wordpress.org/extend/plugins/post-sorting-reloaded/ will let you sort on any field in the wp-posts table but custom fields are not in the table although it does work if you put the priority number in an existing field like excerpt.

    Ideally I would like to sort by priority and then date if it doesn't have a priority. Any suggestions?

    Thanks in advance.

Topic Closed

This topic has been closed to new replies.

About this Topic