• I am working on the custom query that I need to sort the wordpress post by meta_key.
    It is working fine, the problem is now that I need to show all the post but order should be by postmeta.
    It is only showing the posts having that meta_key value in that.

    I need to show all the posts whether that meta_key is present or not and sort the list by post_meta_key.

    Any help will be highly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi navixeldeveloper,

    It might help to see the code. Can you post the code that you’re working with into a pastebin, or a Github gist or similar and post the link here?

    Moderator bcworkz

    (@bcworkz)

    If you are using the WP_Query class to get the posts, you could add an 'orderby' => 'meta_value', argument to the arguments array when you instantiate the class. But then you must also provide a ‘meta_key’ argument, which will exclude posts with no meta value.

    You could get the keyless posts as a separate query and list them either before or after the other posts, which is where they would be anyway if they could have been part of the meta_key query.

    The only other alternative would be to write your own SQL query and execute it using $wpdb methods.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Order post by post meta’ is closed to new replies.