Pull Random Attachments With Pagination

I want to make a archive page that pulls in all uploaded images to the site. I am using the current script. Its paginating somewhat but not showing all the images. Showing around 90 out of 140.

     <?php query_posts(array('posts_per_page'=>'8','paged' => get_query_var('paged'))); ?> 

 <?php
    if ( $images = get_posts(array(
        'post_parent' => $post_id,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'orderby' => 'rand',
        'posts_per_page'=>'8',
        'paged' => $paged,
        'post_mime_type' => 'image',)))
    {
        foreach( $images as $image ) {
            $attachmenturl=wp_get_attachment_url($image->ID);
            $attachmentimage=wp_get_attachment_image_src( $image->ID, postgallery );
            $imageDescription = apply_filters( 'the_description' , $image->post_content );
            $imageTitle = apply_filters( 'the_title' , $image->post_title );

            if (!empty($imageDescription)) {
    echo '<a href="'.$imageDescription .'"><img src="' . $attachmentimage[0] . '" alt=""  /></a>';
} else { echo '


<div class="span3_3">
<div class="widget">
                            <div class="well">
                                <div class="view">
                                    <a href="' .get_attachment_link($image->ID). '" class="view-back" style="display: none;"></a>
                                    <img src="' . $attachmentimage[0] . '" alt="" />
                                </div>
                            </div>
                        </div>
</div>
'; }    }
    } else {
        echo "No Image";
    } ?>
</div>
<div class='pagination pagination-centered'><ul><li>

<!--Pagination-->
<?php echo paginate_links( $args ) ?>
<?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
?>

 </li></ul></div>

Related posts

2 comments

  1. Your since83_pagination() function is assuming the main query– $wp_query— but you are needing to paginate something very different. You need to be paginating the results in $attachments which you are retrieving with get_children(). That was my guess as soon as I saw this question.

    You are going to need to rethink this. There are two options that come to mind.

    If you are happy with passing pagination data in the URL use paginate_links(). That is probably the easiest option. I have written a few answers on using paginate_links.

    Your other option is to use a filter on pre_get_posts. I do not have working code to post but I expect that this will be harder.

    I would say, figure out which route you want to go, get started, and post your new code. I will edit the answer in light of the new code.

  2. If you are using WordPress version 4.5 or greater, you can take advantage of the built-in MySQL function RAND(n) along with its seed value. As long as you pass the same n on each query, you will get the same set of random posts.

    As an example:

    $randomN = 123456;
    
    $posts = get_posts(array(
        //... arguments here
        'orderby' => "rand($randomN)"
    ));
    

    Read more about the RAND(n) function in the docs.

Comments are closed.