WordPress.org

Ready to get started?Download WordPress

Forums

Order By Meta Value Problem (4 posts)

  1. meln
    Member
    Posted 1 year ago #

    Hello,

    I searched but couldn't find a solution to my problem.

    I'm using Magic Fields plugin for custom fields.

    For some post types i added order fields like 'homepage_list_order'. But this field is not required.

    What i'm trying to do is if a value has been entered to 'homepage_list_order' use it if not put the post at the end of the list.

    Let's say i have 10 posts.

    I want to show all of them on my homepage but with my custom order field 'homepage_list_order'.

    I want to enter 'homepage_list_order' value for only 3 of them.

    When i open my homepage the 7 of them is at the top of the list and the 3 of them at the end.

    I guess, when i orderby meta_value_num, WP gets post but if field is empty, WP puts it on the top of list as considering the value is 0. However i want posts that have no meta_value at the end of the list. If i entered value for Post_5, Post_2, Post_9, i want just these 3 at top.

    Sometimes i have 100 posts and just want 3 of them to be at top. Even if i entered a value for these three like 1,2,3 they always are at bottom.

    I hope i could make you understand.

    Any help would be nice.

    Thank you.

  2. bcworkz
    Member
    Posted 1 year ago #

    You might try making the posts with the order field values sticky. I'm not sure how that gets implemented, so couldn't say for sure if this will work.

    You could always add the orderby direction option DESC (descending) to the query argument, then your posts will be listed 3,2,1,empty. You could then swap the 1 and 3 values so the post you want at top has the 3 value and the 3rd from top has the 1 value.

  3. meln
    Member
    Posted 1 year ago #

    Thank you for reply.

    I can do that but it is hard to explain to customer :) If there will no solution probably your suggestion will be best option.

  4. bythegram
    Member
    Posted 1 year ago #

    what if you separated it into two different queries? a query for the posts with the meta key first then a query for the normal posts afterwards?

Topic Closed

This topic has been closed to new replies.

About this Topic