WordPress.org

Ready to get started?Download WordPress

Forums

Order by post-formats (or equivalent?) (8 posts)

  1. Stokkes
    Member
    Posted 1 year ago #

    Hello,

    I am trying to figure out a way to get WordPress' Loop to sort by post-forrmat.

    Specifically, in my Loop, I want all posts to appear in this order:

    1. All posts that have no format (or "standard" posts)
    2. All posts with a post-format (ie: "link", "aside")

    All still sorted by newest first, just anything with a post-format gets pushed down.

    Is this easily done?

    Thanks!

  2. jnhghy
    Member
    Posted 1 year ago #

    Hi, it's not clear to me what you mean by post-format this is not a field in the [prefix]_posts database table so where do you get this value from? it might be theme specific so you'll have to give some extra details about this. If you can share your site link and tell us where you display this info we might figure out a way to loop throw your posts and sort them by post-format.
    Regards

  3. Stokkes
    Member
    Posted 1 year ago #

    Hi jnhghy,

    This is what I mean.

    I would like for posts that do not have a post format to come before posts that do have a post format.

    So for example, let's say today I write 3 posts, 1 of which does not have a post format, the other two that have a post format of "link". When browsing the page, I'd like for my non-post format post to appear first, followed by the two "link" post-format posts, no matter what order they were written.

    In the default loop, posts are sorted by date (this is good), but I want to add another orderby dimension that puts post-formats that contain the same date below the posts that have no format.

    Thanks,

  4. jnhghy
    Member
    Posted 1 year ago #

    So it seems that formats uses post categories to save a post to it's format. Check this 2 threads it will put you on the right track:
    http://wordpress.org/support/topic/exclude-posts-with-certain-post-formats-in-query_posts?replies=5
    http://wordpress.org/support/topic/sort-posts-by-category-2?replies=6

  5. Stokkes
    Member
    Posted 1 year ago #

    Appreciate the help jnhghy...

    I'm not quite sure what I'm asking falls into the links you referred to.

    The first link mentions excluding posts with certain formats, which is not what I want to do. I simply want to order the posts that are in The Loop first by all posts that have no format, then posts that have a format while keeping them ordered by date.

    The second link mentions categories, but really, post formats are not categories and cannot be referenced as such. They do not carry a cat id.

    The default order for posts in the Loop are by date (newest at the top). I still want this, but I want to add a second orderby whereby anything returning has_post_format == TRUE will get put after posts that return false.

    Pseudo Example (What I want):
    Post (No Format - 5pm)
    Post (Format - 6pm)
    Post (Format - 10am)

    Current behaviour:
    Post (Format - 6pm)
    Post (No Format - 5pm)
    Post (Format - 10am)

    Hope this is a bit clearer.

  6. jnhghy
    Member
    Posted 1 year ago #

    OK, I've activated twenty eleven inserted some posts, played with formats and now I know exactly how formats work:
    If the post has no format/standard no changes are made;
    The first time when you select a format (for example "link") to a post wordpress creates a new row in your database in the [prefix]_terms table (the one holding the categories) the name of the new entry is: post-format-link (for the example we chose) same with the slug;
    also in [prefix]_term_relationships (table that tels what post in which category is) a new row is inserted/updated having "object_id" as the post id and term_taxonomy_id as the id that the post-format-link is getting out of the [prefix]_terms table; so basically formats are categories and the links above should help you sort things out.
    Also I've created a pastebin that has a function that I've build for you let me know if it works ... we might include it in a codex page :)
    Regards

  7. Stokkes
    Member
    Posted 1 year ago #

    Hey,

    Thanks for the function. I'm at the office right now so can't really try it. I dove through the wpdb yesterday and found exactly what you're saying about format-link, etc being in prefix_term, prefix_term_relationships etc.

    I could be wrong but you seem to have run into the same issue I'm faced with in your function. You first query all the non formatted post then the formatted post. What this will result in (If I'm not wrong) is posts displayed with no format first (all of them) then posts displayed with a format (all of them).

    It's close, but not quite there (and where I'm getting hung up). For each day, I want to display posts in this manner. Let's take my previous example above, but go a bit further.

    New example:
    8 posts, 3 from Today, 3 from yesterday and 2 from 2 days ago The way I'd like it displayed is as follows (the way you'd see it when browsing the blog):

    Today's Post (No Format - 6pm)
    Today's Post (Format - 10pm)
    Today's Post (Format - 11am)
    Yesterday's Post (No Format - 3pm)
    Yesterday's Post (No Format - 10am)
    Yesterday's Post (Format - 7pm)
    2Day's Ago Post (Format 9pm)
    2Day's AGo Post (Format 2pm)

    So the default chronological order is what I want, but I want to "shove" all the formatted posts (with ie: post-format-link) to the bottom of the day and leave the non-formatted post above. My reasoning is that non-formatted posts (or "Articles") as I'll call them have more weight than frivolous formatted post which is why I want them to appear on top. What I don't want is a list of let's say 10 non-formatted post (all from different days) and then 10 formatted posts (all from different days). They should still be grouped by day.

  8. jnhghy
    Member
    Posted 1 year ago #

    Hmmm ... got it, you were clear from the first post but I've miss-understood from what you want I'm pretty sure query_posts won't work there's no way to make WP_Query return posts ordered by day/category/date again .... instead you'll have to build your own selection of posts you can get info here.
    I hope that until tomorrow I'll find time and try to link here a pastebin that will do what you require but I won't promise anything.
    Regards

Topic Closed

This topic has been closed to new replies.

About this Topic