1

I want to create a custom loop inside a single-ediciones.php file and I need pagination (actually AJAX pagination).

I need my normal loop because I need to show some data of the CPT "Ediciones" and I want a custom loop to show some CPT related to the current CPT via ID.

Actually, I don't know if is possible have a normal loop and inside a custom loop with pagination.

Right now I can get the "ALL_THE_URL/page/2" work, but without pagination. Even using new WP_Query and adding the get_next_posts_link( 'Older Entries', $the_query->max_num_pages );.

<?php if ( have_posts() ) :
    while( have_posts() ) : the_post();
        $edicion_ID = get_the_ID();
        $edicion_titulo = get_the_title();
        $edicion_titulo = strtolower($edicion_titulo); ?>
        <div class="container ediciones-detalle-contenedor">
            <div class="row">
                <div class="col-md-4 col-md-push-8 col-sm-4 col-sm-push-8">
                    <h3><?php echo $edicion_titulo; ?></h3>
               </div>

               <div class="col-md-8 col-md-pull-4 col-sm-8 col-sm-pull-4">
                   <div class="row">
                        <?php 
                        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                        $args = array(
                            'numberposts'   => 4,
                            'post_type'     => array('temas', 'perfiles', 'udem-mundo', 'deportes', 'cultura'),
                            'meta_query'    => array(
                                'relation'      => 'AND',
                                array(
                                    'key'       => 'contenido_relacionado_impreso',
                                    'value'     => 'si',
                                    'compare'   => '=',
                                ),
                                array(
                                    'key'       => 'contenido_relacionado_edicion',
                                    'value'     => $edicion_ID,
                                    'compare'   => '=',
                                ),
                            ),
                            'paged'         => $paged,
                        );

                        $posts_relacionados_edicion = get_posts($args);
                        if ( !empty($posts_relacionados_edicion) ) :
                            foreach ($posts_relacionados_edicion as $post) : setup_postdata( $post ); ?>
                                <div class="col-md-6 col-sm-6">
                                    <a href="<?php the_permalink(); ?>" class="blog-titulo-chico"><h3><?php the_title(); ?></h3></a>
                                </div>
                            <?php endforeach;
                            wp_reset_postdata();
                        endif; ?>
                    </div>
                </div>
            </div>
        </div>
    <?php endwhile;
endif; ?>
4

As I far I know, get_posts() function can not be used to paginated results. It is intended to get an array of posts and it sets no_found_rows to true by default from WP 3.1. Pagination won't work unless you set it to false, or better, use WP_Query.

numberposts is also a invalid argument.

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

$args = array(
    'posts_per_page'   => 4,
    'post_type'       => array('temas', 'perfiles', 'udem-mundo', 'deportes', 'cultura'),
    'meta_query'    => array(
        'relation'      => 'AND',
         array(
            'key'       => 'contenido_relacionado_impreso',
            'value'     => 'si',
            'compare'   => '=',
         ),
         array(
             'key'       => 'contenido_relacionado_edicion',
             'value'     => $edicion_ID,
             'compare'   => '=',
         ),
     ),
     'paged'         => $paged,
 );

 $posts_relacionados_edicion = new WP_Query( $args );

 if ( $posts_relacionados_edicion->have_posts() ) :

     while( $posts_relacionados_edicion->have_posts() ) :

         $posts_relacionados_edicion->the_post(); ?>

         <div class="col-md-6 col-sm-6">
                 <a href="<?php the_permalink(); ?>" class="blog-titulo-chico"><h3><?php the_title(); ?></h3></a>
              </div>

      <?php endwhile;

      echo paginate_links( array(
          'current' => $paged,
          'total' => $posts_relacionados_edicion->max_num_pages,
          'format' => '?page=%#%'
      ) );

       wp_reset_postdata();

  else : ?>

     <?php _e( 'No found posts.', 'textdomain' ); ?>

  <?php endif; ?>

The code above generates paginate links in the format example.com/{post-name}/{page-number} and it seems to be working.

The next code generates paginate links in the format example.com/{post-name}/page/{page-number}, but they are redirect to example.com/{post-name}/ and makes pagination not working. More work and test would be needed if you want to use URLs like example.com/{post-name}/page/{page-number}:

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

$args = array(
    'posts_per_page'   => 4,
    'post_type'       => array('temas', 'perfiles', 'udem-mundo', 'deportes', 'cultura'),
    'meta_query'    => array(
        'relation'      => 'AND',
         array(
            'key'       => 'contenido_relacionado_impreso',
            'value'     => 'si',
            'compare'   => '=',
         ),
         array(
             'key'       => 'contenido_relacionado_edicion',
             'value'     => $edicion_ID,
             'compare'   => '=',
         ),
     ),
     'paged'         => $paged,
 );

 $posts_relacionados_edicion = new WP_Query( $args );

 if ( $posts_relacionados_edicion->have_posts() ) :

     while( $posts_relacionados_edicion->have_posts() ) :

         $posts_relacionados_edicion->the_post(); ?>

         <div class="col-md-6 col-sm-6">
                 <a href="<?php the_permalink(); ?>" class="blog-titulo-chico"><h3><?php the_title(); ?></h3></a>
              </div>

      <?php endwhile;

      echo paginate_links( array(
          'current' => $paged,
          'total' => $posts_relacionados_edicion->max_num_pages
      ) );

       wp_reset_postdata();

  else : ?>

     <?php _e( 'No found posts.', 'textdomain' ); ?>

  <?php endif; ?>

Or you could also build your own pagination endpoint:

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

$args = array(
    'posts_per_page'   => 4,
    'post_type'       => array('post'),
    'paged'         => $paged,
 );

 $posts_relacionados_edicion = new WP_Query( $args );

 if ( $posts_relacionados_edicion->have_posts() ) :

     while( $posts_relacionados_edicion->have_posts() ) :

         $posts_relacionados_edicion->the_post(); ?>

         <div class="col-md-6 col-sm-6">
                 <a href="<?php the_permalink(); ?>" class="blog-titulo-chico"><h3><?php the_title(); ?></h3></a>
              </div>

      <?php endwhile;

      echo paginate_links( array(
          'current' => $paged,
          'total' =>  $posts_relacionados_edicion->max_num_pages,
          'format' => '?my_page=%#%'
      ) );

       wp_reset_postdata();

  else : ?>

     <?php _e( 'No found posts.', 'textdomain' ); ?>

  <?php endif; ?>

Combined with the rewrite tag:

add_action('init', function() {
    add_rewrite_tag( 'my_page', '([0-9]+)' );
} );
  • This will not work unfortunately, numberposts is invalid in WP_Query, WP_Query only accepts posts_per_page and showposts (which is not depreciated as the codex incorrectly states). Secondly, single post pages (just like static front pages) uses the page query var, and not paged like other pages/archives. My answer here might also be useful in answering the question – Pieter Goosen Apr 29 '16 at 5:20
  • Opps, I just copied numberposts that from the question. Thanks Pieter. As far I understand, page query var is used for single post pagination; not for pagination of a loop, not matter if the loop is in a single template. Let me do some tests. – cybmeta Apr 29 '16 at 9:21

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.