Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Hook ‘pre_get_posts’ and set the ‘post_type’ query var to be an array of all desired post types. By default the results will be chronological order regardless of post type. If you must see all three different post types before seeing the next group, regardless of publish date, you’d need some sort of buffered output code that keeps track of the next available post of each type and searches ahead to ensure all post types are output once before reaching into the buffer for the next group.

    Thread Starter jaiunblog

    (@jaiunblog)

    Thank you bcworkz,
    I can’t edit my question but this is not 10 posts but more something like 24 A,42 B,30 C…
    I try to simplify my question but I do it wrong :-/

    I don’t know if it’s better way but 3 new WP_Query for each type, $myquery->found_posts to check the count and some while and if to do my loop ?

    Moderator bcworkz

    (@bcworkz)

    I suppose it all depends on how one defines “better way”. Easiest to code or most efficient for the server? Three queries is probably easiest to code, especially if pagination is involved.

    Most efficient is to get SQL to return all the results in the sequence you want. I don’t know enough SQL to actually do that, but I think it’s possible.

    A hybrid approach where we still use one query, but rely on PHP to do the ordering is reasonably efficient, but harder to code. There might be a way to order the results with usort(). The callback would need to implement a set of sort rules that ends up ordering all the posts correctly. This could be tricky to get right, but it’d be pretty cool.

    Alternately, with a single query, like I first suggested, implement a read ahead buffer that keeps track of the indexes for the next available posts of each type. Then output from each buffer in rotation. This should be fairly efficient, but it’s hard to explain how this would work exactly.

    In a huge site with many hundreds of posts, it’d be worth finding an efficient method. For a hundred some posts, you’re fine using the three queries, unless you like to tinker with code just for fun ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘rearrange and make a sequence by type’ is closed to new replies.