I'm trying to create a custom page template that displays:

  1. A sticky post

  2. A random post with category "text"

  3. A random attachment from the media gallery (not attached to a post) with category "photo."

  4. A random post with category "text"

... and so on, until there are no more text posts. At this point, the page should display photos in random order until there are no more photos.

A few things: I'm using a plugin ("Media Library categories") that allows me to assign categories to items in the media library. All of the posts on this page are set inside of a jquery slider.

The code below is based on the solution outlined here: Custom query - alternate posts by category. It would work perfectly if I were trying to return images that are inside of posts (I first tested it this way), but it doesn't successfully retrieve attachments from the media library that aren't attached to posts.

$sticky_post = get_option( 'sticky_posts' );

$args1 = array(
    'post_type' => 'post',
    'orderby' => 'rand',
    'category_name' => 'text',
    'post_status' => 'published',
    'post__not_in' => $sticky_post,
    'posts_per_page' => -1,
    'fields' => 'ids'
$query1 = get_posts( $args1 );

$new_posts_array1 = [];

if( $query1 ) {
    $counter1 = 0;

    foreach ( $query1 as $post ) {
        $new_posts_array1[$counter1++ * 2] = $post;
    unset( $post );

$args2 = array(
    'post_type' => 'attachment',
    'category_name' => 'photo',
    'orderby' => 'rand',
    'post_status' => 'any',
    'post__not_in' => $sticky_post,
    'posts_per_page' => -1,
    'fields' => 'ids'
$query2 = get_posts( $args2 );

$new_posts_array2 = [];

if( $query2 ) {
    $counter2 = 0;

    foreach ( $query2 as $post ) {
        $new_posts_array2[($counter2++ * 2) + 1] = $post;
    unset( $post );

$new_posts_array = $new_posts_array1 + $new_posts_array2;
ksort( $new_posts_array );
$q = array_merge( $sticky_post, $new_posts_array );
<div id="content-slider" class="royalSlider contentSlider">
<?php $args = array(
'post_type' => array('attachment', 'post'),
'posts_per_page'        => -1,
'post__in'              => $q,
'ignore_sticky_posts'   => 1,
'orderby'               => 'post__in',
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
</div><!-- close royalSlider-->

I know that using the_content() wouldn't display those attachment images, but when I do a var_dump of the merged query, it only shows the text posts anyway, so I know that the problem isn't just in displaying the images, but in the array_merge itself.

Is this approach possible with media gallery attachments? If not, is there another way to achieve the same goal?

Thanks for any advice.

1 Answer 1


I figured it out. It was a series of silly mistakes, really. $args1 should be 'post_status' => 'publish', [not 'published']. $args for $query should include 'post_status' => array( 'publish', 'inherit' ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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