0

so been looking around for this answer, but no go -- basically i have a WP Query that is incased in a shortcode.. I have set up the paginate links function which outputs it on the page. However, the page url does not change when clicked (href does show the correct url -- /page/2) I know it is something with the main Query when executed in a shortcode. Any help would be greatly appreciated.

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(
    'post_type' => $type,
    'posts_per_page' => -1,
    'orderby' => 'rand',
    'paged' => $paged

);

    $the_query = new WP_Query($args); 

// Put default query object in a temp variable
$tmp_query = $wp_query;
// Now wipe it out completely
$wp_query = null;
// Re-populate the global with our custom query
$wp_query = $the_query;

?>




    <div class="row">
<?php  if ( $the_query->have_posts() ) :  $i=0; ?>



    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
        $sphere = get_post_meta(get_the_id(),'testimonial_profession', true);
        if ($i == 2) {
        $i = 0;
        ?>
        </div>
        <div class="row">
        <?php
    }

    ?>
        <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="testimonial_inner_wrapper">
                                <?php if(!has_post_thumbnail()): ?>
                                <h4 class="testimonials-inner-title"><?php the_title(); ?></h4>
                                <?php if(!empty($sphere)): ?>
                                    <div class="testimonial_sphere"><?php echo($sphere); ?></div>
                                <?php endif; ?>
                                <div class="short_separator"></div>
                            <?php else: ?>
                                <div class="media">
                                    <div class="media-left media-top">
                                        <div class="testimonial-media-unit">
                                            <?php the_post_thumbnail('img-69-69', array('class'=>'testimonial-media-unit-rounded')); ?>
                                        </div>
                                    </div>
                                    <div class="media-body">
                                        <h4 class="testimonials-inner-title"><?php the_title(); ?></h4>
                                        <?php if(!empty($sphere)): ?>
                                            <div class="testimonial_sphere"><?php echo($sphere); ?></div>
                                        <?php endif; ?>
                                        <div class="short_separator"></div>
                                    </div>  
                                </div>
                            <?php endif; ?>
                                                        <div class="testimonial_inner_content" style="color:#aaaaaa"><p><?php the_excerpt(); ?></p>
</div>
                        </div> <!-- inner wrapper -->
                    </div>


    <?php $i++; endwhile; ?>
    <!-- end of the loop -->
</div><div class="poNav">
     <?php

$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' => $the_query->max_num_pages
) );

    $wp_query = NULL;
$wp_query = $temp_query;
?></div>

    <?php wp_reset_postdata(); ?>
    <?php
    $myvariable = ob_get_clean();
    return $myvariable; ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; 
}
1
  • 1
    Where do you set $args in the first line?
    – Milo
    May 5, 2017 at 22:06

1 Answer 1

0

Your $args variable would be helpful.

As I can see, you are creating custom WP_QUERY, so get_query_var('paged') isn't related to your $the_query = new WP_QUERY, but to global $wp_query.

Possible solution: declare global $paged and add it to $args = array( /* your args here*/, 'paged' => $paged );, also change your get_query_var('paged') to $paged

2
  • added the $args to the code
    – DEM
    May 6, 2017 at 23:48
  • 1
    just replace $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; to global $paged;. If it won't work it's more possible that you have written something wrong in paginate_links() function.
    – SopsoN
    May 7, 2017 at 12:38

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.