Evenly select Records on categorical column with Repeating Sequence and pagination in Postgres

Database: Postgres
I have a product(id, title, source, ...) table which contains almost 500K records. An example of data is:

| Id | title    | source   | |:---|---------:|:--------:| | 1  | product1 | source1  | | 2  | product2 | source1  | | 3  | product3 | source1  | | 4  | product4 | source1  | | .  | ........ | source1  | | .  | ........ | source2  | | x  | productx | source2  | |x+n |productX+n| sourceN  | 

There are are 5 distinct source values. And all records have source values random.

I need to get paginated results in such a way that: If I need to select 20 products then the results set should contain results equally distributed based on source and should be in a repeating sequence. 2 products from each source till the last source and again next 2 products from each source. For example:

| #  | title    | source   | |:---|---------:|:--------:| | 1  | product1 | source1  | | 2  | product2 | source1  | | 3  | product3 | source2  | | 4  | product4 | source2  | | 5  | product5 | source3  | | 6  | product6 | source3  | | 7  | product7 | source4  | | 8  | product8 | source4  | | 9  | product9 | source5  | | 10 |product10 | source5  | | 11 | ........ | source1  | | 12 | ........ | source1  | | 13 | ........ | source2  | | 14 | ........ | source2  | | .. | ........ | .......  | | 20 | ........ | source5  | 

What is the optimized PgSql query to achieve the above scenario considering the LIMIT, OFFSET, sources can increase or decrease?

WordPress Sub Category Archive, Show Extra Empty Page Number in Pagination

This my code for sub category archive page:

                        <?php $  my_query = new WP_Query( array( 'posts_per_page' => 12, 'post_type' => 'post', 'paged' => get_query_var( 'paged' ), 'post_status' => 'publish', 'category__and' => array( 2 ) ) ); ?>                                 <?php if ($  my_query->have_posts()) : ?>                         <?php while ($  my_query->have_posts()) : $  my_query->the_post(); ?>                          //Contents Here//                          <?php endwhile; ?><?php endif; ?>                                            <?php wp_reset_postdata(); ?> 

Now I have 1 empty extra page number in pagination of my archive page.

For example I have only 3 pages of contents by I can See a number 4 link in pagination numbers who open’s an empty page without any posts in there.

Can you help me please?

Post pagination links ordered by meta value

Is there a way to order the nex-previous post links, or any other pagination function to get the links ordered by meta value, like I have done in the posts listing page shown below?

$  events = get_posts(array(     'posts_per_page' => -1,     'post_type' => 'post',     'meta_key'  => 'date_time',     'orderby'   => 'meta_value',     'order'     => 'ASC',     'suppress_filters' => false )); 

The main goal is tho have the next, – previous event buttons in chronological order in the single post page. (not the post date, but the event date that is stored in custom fields)

Move to pagination with URL parameters

I can not find the way to use URL parameters to define pagination pages /posts?page=2 instead of the conventional WordPress way /posts/page/2/

The goal is to make WordPress change the way it manages pagination, without breaking the website that is using the conventional WordPress permalinks format

/posts/page/2/

and change the pagination to

/posts?page=2

I am happy also with this variant:

/posts/page/2?page=2

if necessary.

Include a leading zero in pagination

I’m new to WordPress Development and have been teaching myself over the last few months how to develop a theme from scratch. Normally I can figure issues out on my own through forums, but I can’t really find much on this one.

I’ve been trying to add leading zeros to my pagination if the number is less than 10.

I.e: < Newer 01 02 03 … 10 Older >

Here is my pagination code. Any pointers in the right direction would be appreciated!

if ( !function_exists('palfrey_pagination') ) { function palfrey_pagination( $  range = 5 ) {      if( is_singular() )         return;      // $  paged - number of the current page     global $  paged, $  wp_query;      // Stop execution if there's only 1 page.     if( $  wp_query->max_num_pages <= 1 )         return;       $  paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;      if ( !$  max_page )         $  max   = intval( $  wp_query->max_num_pages );      if ( $  max_page > 1 )             if ( !$  paged ) $  paged = 1;       // Add current page to the array.     if ( $  paged >= 1 )         $  links[] = $  paged;       // Add the pages around the current page to the array.     if ( $  paged >= 3 ) {         $  links[] = $  paged - 1;         $  links[] = $  paged - 2;     }       if ( ( $  paged + 2 ) <= $  max ) {         $  links[] = $  paged + 2;         $  links[] = $  paged + 1;     }          // The pagination     echo "\n" . '<div class="content-block-common-large">                     <div class="block-wrap">                         <div class="inline-group flex-group relative align-center column-align-bottom">                             <div class="column responsive width-450">                                 <ul class="pagination display-block relative align-center width-1of1">' . "\n";       // Link to 'Newer' posts.     if ( get_previous_posts_link() ) {         printf( '<li>%s</li>' . "\n", get_previous_posts_link( '<div class="display-inline-block relative float-left"><span class="pagination-prev">Newer</span></div>' ) );     }else{     echo '<li><div class="display-inline-block relative float-left pointer-events-none" style="opacity: .6;"><span class="pagination-prev">Newer</span></div></li>';     }          // Link to first page, plus ellipses if necessary.     if ( ! in_array( 1, $  links ) ) {         $  class = 1 == $  paged ? ' class="active"' : '';           printf( '<li%s><a href="%s">%s</a></li>' . "\n", $  class, esc_url( get_pagenum_link( 1 ) ), '1' );           if ( ! in_array( 2, $  links ) )             echo '<li>…</li>';     }       // Link to current page, plus 2 pages in either direction if necessary.     sort( $  links );     foreach ( (array) $  links as $  link ) {         $  class = $  paged == $  link ? ' class="active"' : '';         printf( '<li%s><a href="%s">%s</a></li>' . "\n", $  class, esc_url( get_pagenum_link( $  link) ), $  link );     }       // Link to last page, plus ellipses if necessary.     if ( ! in_array( $  max, $  links ) ) {         if ( ! in_array( $  max - 1, $  links ) )             echo '<li>…</li>' . "\n";           $  class = $  paged == $  max ? ' class="active"' : '';         printf( '<li%s><a href="%s">%s</a></li>' . "\n", $  class, esc_url( get_pagenum_link( $  max) ), $  max );     }       // Link to 'Older' posts.     if ( get_next_posts_link() ) {         printf( '<li>%s</li>' . "\n", get_next_posts_link( '<div class="display-inline-block relative float-right"><span class="pagination-next">Older</span></div>' ) );     }else{     echo '<li><div class="display-inline-block relative float-right pointer-events-none" style="opacity: .6;"><span class="pagination-next">Older</span></div></li>';     }       echo "\n" . '           </ul>                         </div>                     </div>                 </div>             </div>' . "\n";       } } 

Pagination (archive posts) getting out of hand – what to do from an SEO POV?

WordPress as many of us know creates an archive loop of old posts.

From an SEO POV, some of my categories are now at 100+ pages which is a lot of bloat…

Sure, I can set each of these paginated pages a canonical link but still, feels like it is a bit unnecessary for all these indexed pages that just contain a title and an excerpt (which is basically duplicate content..)

Is one approach to simply switch off archive loops or does Google ignore these archived pages?

Thanks

Remove pagination from e-commerce category page while preserving SEO text relevance

I am administering an e-commerce website where currently we have pagination implemented on every category page.

On the first page of each category, we have SEO text in the bottom – a description of the category which helps our page to rank higher in search results. In many cases, it is long enough to make it impractical to place it at the top of the page.

I am flirting with the idea of converting our structure to an “infinite scroll” experience, to remove pagination altogether. I realise that this would yield some SEO benefits.

However, I am seriously worried that a lengthy list of products would dilute the importance of our category description at the bottom. I remember reading somewhere that Google places more importance on the content which is higher on the page.

Has anyone tried to implement the “infinite scroll” and did you notice any serious changes in your ranking? Or maybe you have an idea as to how it will affect the relevance of the category description at the bottom of the page?

Two Pagination in One page with shortcode

I create a shortcode to show posts by category with attributes. I want to show 2 separate categories with a separate shortcode in one page but the pagination connected.

If I change pagination in the first shortcode, also change pagination in the second shortcode. Basically it same shortcode but different attributes.

here my code:

add_shortcode('new-blog-grid','new_blog_grid'); function new_blog_grid($  atts){     ob_start();     extract( shortcode_atts(                array(                        'count' => '',                        'pagination' => '',                        'category' => ''                                   ),                $  atts        ));      if ($  count) {$  post_count = $  count; } else {$  post_count = 12; }     $  paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;         $  args = array(         'post_type' => 'post' ,         'posts_per_page' => $  post_count,         'post_status' => 'publish',         'paged' => $  paged,         'ignore_sticky_posts' => true,         'order' => 'DESC',          'orderby' => 'date'     );      if($  category != null) {         $  args['category_name'] = $  category;     }       $  query = new WP_Query($  args);      echo '<div class="listing-blog-grid">';          if ($  query->have_posts()) {             while ($  query->have_posts()) {                 $  query->the_post();                  $  thumbs = get_the_post_thumbnail_url();                 $  terms = get_the_terms(get_the_ID(),'category');                 //print_r($  terms);                 ?>                 <div class="listing-blog-list" onclick="location.href='<?php the_permalink()?>'" style="cursor:pointer;">                     <div class="listing-blog-wrapper">                         <div class="listing-blog-header">                             <div class="listing-blog-images" onclick="location.href='<?php the_permalink()?>'" style="background: url('<?php echo $  thumbs; ?>') no-repeat center; background-size:cover; background-color: rgba(0, 0, 0, 0.5);"></div>                         </div>                         <div class="listing-blog-content">                             <div class="listing-date"><?php the_time( 'j F Y ' ); ?></div>                                                       <?php the_title( '<h4 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h4>' ); ?>                         </div>                      </div>                               </div>         <?php   }         }          else {             echo "Geen berichten van deze auteur.";         }      echo '</div>';      if ($  pagination == 'yes') {  ?>                   <div class="pagination">             <?php             $  total_pages = $  query->max_num_pages;             $  current_page = max( 1, get_query_var('paged'));              if ($  current_page != 1){                  echo '<a href="/new-landing-page/"><img src="/wp-content/uploads/2019/06/paginatie-B-left.png"></a>';             }              if ($  total_pages > 1){                        echo paginate_links(array(                     'base' => get_pagenum_link(1) . '%_%',                     'format' => '/page/%#%',                     'current' => $  current_page,                     'total' => $  total_pages,                     'prev_text' => __(''),                     'next_text' => __(''),                 ));             }             if ($  current_page != $  total_pages){                 echo '<a href="/new-landing-page//page/'.$  total_pages.'"><img src="/wp-content/uploads/2019/06/paginatie-B.png"></a>';             }               ?>                                </div>          <?php }     return ob_get_clean();     wp_reset_query(); } 

so shortcode I put in one page

cat: media

[new-blog-grid count=5 pagination=yes category=pr-media] 

cat: stories

[new-blog-grid count=5 pagination=yes category=stories] 

I need it became seperate pagination, is there any way to achieve that.