Support

Account

Home Forums General Issues How to order by custom field then post date?

Solved

How to order by custom field then post date?

  • I’m seeing how to select posts by a custom field or the post date, but not both. The post date is of course the date the post was created (or manually changed to on the Posts list), and the custom field would be an ACF field named say “Gallery Image”.

    So I want to show posts with a gallery image at the top, sorted by date, then the rest to show up afterword sorted by their date.

    Is this possible, and how should I do this? There seems to be a mix of orderyby and meta_value maybe that I’m not understanding, though I don’t know if that’s how I should be doing it.

  • You need to use clauses in your meta_query in order to combine ordering by meta values and post date. See the last code snippet in the orderby section https://codex.wordpress.org/Class_Reference/WP_Query

    As far as ordering posts based on whether or not the post has an image or a gallery, that I don’t really know. There is nothing like this listed and I don’t know if any of the existing “compare” arguments will do this. I would probably create a true/false field and make the gallery or image field conditional base on the true/false field. I would then set the image field to required so that if the user selects to add image that image is required. Then I would order the posts by the true/false field in DESC order. Since the db value is 0/1, ordering them this way would put any with the field set to true at the top.

  • Sorry, I should specify that Gallery Image is just a checkbox, not an image. It really just signifies to show the posts marked as “Gallery” at the top of the page with their featured image (otherwise just an icon shows in the post block on the list in differently-formatted HTML).

  • a checkbox field is stored as a serialized array, unless it’s a radio field. Sorry, it’s hard to tell from the explanation. anyway, for checkbox fields you’s need to use a “LIKE” query on the meta field. Unless it’s a true/false field, which looks like a checkbox, in this case see my previous comment.

  • Field type is True/False… all the rest of it is just the defaults. I’m not seeing that it means it’s a serialized array or radio field or anything, all I know is it’s True/False and I can do an orderby query for it by itself or by the post date by itself but not sure how to integrate it with a date orderby where the checked items of “Gallery” true show at the top then the secondary order is by post date.

    https://i.imgur.com/m1GdSfe.png

    My SQL would look like SELECT * FROM POSTS ORDER BY GALLERY, DATE.

    So the posts would come out like this:

    Post 5 (is Gallery) date Jan 19
    Post 7 (is Gallery) date Jan 17
    Post 3 (is not Gallery) date Jan 20
    Post 4 (is not Gallery) date Jan 12

  • You need to create a meta_query that includes a clause and then use that clause in your orrderby. See the last code snippet in the orderby section https://codex.wordpress.org/Class_Reference/WP_Query. Of interest here is the “city_clause” example.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘How to order by custom field then post date?’ is closed to new replies.