1

I have tried several plugins which seem to purport to do this (like this one), but they either don't or are very poor in their implementation and cause problems (the one linked seems to randomly select.

I have a WP_Query in my index.php as follows:

<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');  
if ( get_query_var('page') ) $paged = get_query_var('page');

$query = new WP_Query( array( 'post_type' => array('post','secondposttype'), 'paged' => $page ) );

if ( $query->have_posts() ) : ?>

As such, my homepage displays ordinary posts and posts from the custom post type "secondposttype".

I am also using conditional count scripts, so I can customise what appears, etc:

<?php if ($count == 1) : ?>

The solution we currently use is the Q2W3 Post Order plugin, which works great if there is just one post type. I have tried automatically adding a custom taxonomy value to new posts/custom post type posts, querying that custom taxonomy in my index.php and then using Q2W3 Post Order to order posts within that taxonomy, but it simply doesn't work (plus, this feels much more hacky than it should be).

My question is: is there a way to order posts and custom post types as one big list? Such that, when I query them like above, I can order how they are displayed? I would find it hard to believe that wanting to do this is a rare occurrence.

EDIT: I'm going to give an example of what I want to do. I want to display posts and posts from the second post type on the homepage through one WP_Query. And I want to be able to control it that so the order could go like this:

  1. Post 1 from "post" post type
  2. Post 1 from "secondposttype" post type
  3. Post 2 from "secondposttyle" post type
  4. Post 2 from "post" post type
  5. Post 3 from "secondposttype" post type

And then, on another day, I might want the order to be:

  1. Post 3 from "secondposttype" post type
  2. Post 1 from "post" post type
  3. Post 2 from "post" post type"
  4. Post 1 from "secondposttype" post type
  5. Post 2 from "secondposttype" post type

There are plugins which order one post type, so that you can decide which post appears first, second, third, fourth, etc. I just want to be able to group posts and some other post types so that not only do they appear in one stream (which is possible through WP_Query) but that they can be ordered in whatever order and that posts and posts from the second post type are treated as one and the same for ordering purposes.

2

In terms of the query, it's pretty straightforward to do this, but you'll have to come up with your own interface for creating the order of posts, which should ultimately save the post IDs to an array in the order you want them to appear:

$post_ids = array( 42, 13, 23, 99 );

Then it's just a matter of getting a "page" of those IDs:

// current page number
$paged = 1;
// number of posts per page
$posts_per_page = 2;
// starting position
$offset = ( $paged - 1 ) * $posts_per_page;
// extract page of IDs
$ids_to_query = array_slice( $post_ids, $offset, $posts_per_page );

Then pass those IDs to the query via post__in, and orderby the post__in array:

$args = array(
    'post_type' => array( 'post','secondposttype' ),
    'post__in' => $ids_to_query
    'orderby' => 'post__in',
    'ignore_sticky_posts' => 1,
    'posts_per_page' => $posts_per_page
);
$ordered_posts = new WP_Query( $args );
  • This is absolutely fantastic and exactly what I'm looking for. Thank you! I just tested it out with a hard-coded array there and it works perfectly. The only trouble now for me is to figure out how to develop a plugin that can do the array for me. Thanks again. – Edmund Heaphy Jun 28 '14 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.