Skip to main content
Edited in all attempts and formatted them, sharing all insight I gathered from it. Also removed pagination functions because they serve no purpose to problemsolving
Source Link

The idea was to manually setreset paged for the main query pagemain query when it exceeds max_num_pagesmax_num_pages and store the "actual page" in global $original_page_request for everything that needs itmy $secondloop to set its paged parameter to.

I then edited authorUnfortunately though, it doesn't work.php:

global $original_page_request;
// Check if max_num_pages is exceeded (= if $original_page_request is set!)
// Also set the $paged variable for my $secondloop to the unaltered paged query var.
if (empty($original_page_request)) {
    $is_within_max = true;
    $paged = get_query_var('paged') ? intval(get_query_var('paged')) : 1;
} else {
    $is_within_max = false;
    $paged = $original_page_request;
}

// Main loop, hide it if max_num_pages exceeded!
if ($is_within_max && have_posts()) { ?>
    <div class="row archive-grid">
        <?php while (have_posts()) : the_post();
            include(locate_template('parts/loop-archive-grid.php'));
        endwhile; ?>
    </div>
<?php }

Problem is, it doesn't work. I was so happy not to see a 404 any more, but now it automatically sets the main query page to 0.

0?! Why 0?!

When hooking intoBecause in pre_get_posts, thiscode is exactly whatexecuted before WordPress got its posts. There's access to $query->max_num_pages gives me. That's because by that timebut that's just for setting query variables, it's still in the process of gathering query variables and did not execute its query YET. So there are no posts to show because no posts retreived, and thus no max_num_pages to calculate from.

So that means I have no idea when I'm about to exceed max_num_pages.

I then copied the new code from my_post_queries( $query ) to another function which hooks into wp. There, I have access to max_num_pagesmax_num_pages is a valid number and also the template I'm in. Still nothingis_author() is correct too. Because asAs soon as I exceed the number of posts, said code doesn't get executedstill no luck. 404. I could however set $wp_query->is_404 = false but that returns me to the index, yet again.

The idea was to manually set the main query page when it exceeds max_num_pages and store the "actual page" in global $original_page_request for everything that needs it.

I then edited author.php:

global $original_page_request;
// Check if max_num_pages is exceeded (= if $original_page_request is set!)
// Also set the $paged variable for my $secondloop to the unaltered paged query var.
if (empty($original_page_request)) {
    $is_within_max = true;
    $paged = get_query_var('paged') ? intval(get_query_var('paged')) : 1;
} else {
    $is_within_max = false;
    $paged = $original_page_request;
}

// Main loop, hide it if max_num_pages exceeded!
if ($is_within_max && have_posts()) { ?>
    <div class="row archive-grid">
        <?php while (have_posts()) : the_post();
            include(locate_template('parts/loop-archive-grid.php'));
        endwhile; ?>
    </div>
<?php }

Problem is, it doesn't work. I was so happy not to see a 404 any more, but now it automatically sets the main query page to 0.

0?!

When hooking into pre_get_posts, this is exactly what $query->max_num_pages gives me. That's because by that time, it's still in the process of gathering query variables and did not execute its query YET. So there are no posts to show because no posts retreived, and thus no max_num_pages.

I then copied the new code from my_post_queries( $query ) to another function which hooks into wp. There, I have access to max_num_pages and also the template I'm in. Still nothing. Because as soon as I exceed the number of posts, said code doesn't get executed. I could however set $wp_query->is_404 = false but that returns me to the index, yet again.

The idea was to manually reset paged for the main query when it exceeds max_num_pages and store the "actual page" in global $original_page_request for my $secondloop to set its paged parameter to. Unfortunately though, it doesn't work. I was so happy not to see a 404 any more, but now it automatically sets the main query page to 0. Why 0?!

Because in pre_get_posts, code is executed before WordPress got its posts. There's access to $query but that's just for setting query variables, there are no posts retreived, and thus no max_num_pages to calculate from.

So that means I have no idea when I'm about to exceed max_num_pages.

I then copied the new code from my_post_queries( $query ) to another function which hooks into wp. There, max_num_pages is a valid number and is_author() is correct too. As soon as I exceed the number of posts, still no luck. 404. I could set $wp_query->is_404 = false but that returns me to the index yet again.

Edited in all attempts and formatted them, sharing all insight I gathered from it. Also removed pagination functions because they serve no purpose to problemsolving
Source Link

THE CODE:

author.php (just the useful bit)

<?php if (have_posts()) { ?>
    <div class="row archive-grid">
        <?php while (have_posts()) : the_post();
            include(locate_template('parts/loop-archive-grid.php'));
            endwhile; ?>
    </div>
<?php }

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$secondloop = new WP_Query(array(
    "post_type"      => "musikalbum",
    "posts_per_page" => 3,
    "author_name"    => get_query_var('author_name'),
    "paged"          => $paged
));

if ($secondloop->have_posts()) { ?>
    <div class="row archive-grid">
        <?php while ($secondloop->have_posts()) : $secondloop->the_post();
            get_template_part( 'parts/loop', 'album-grid' );
        endwhile; ?>
    </div>
    <?php author_page_navi( $loop );
} else {
    joints_page_navi();
} ?>

Setting the posts_per_page for my main query in my functions.php

function my_post_queries( $query ) {
  if (!is_admin() && $query->is_main_query()){
    if (is_author()){
      $query->set('posts_per_page', 3);
    } else {
      $query->set('posts_per_page', 9);
    }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

But it still shows me a 404 error. If I remove the if clause, then it leads me to the index.php which is not reeeally what I intended. My guess isSo I need that if clause...but WordPress doesn't seem to know it yet!

  1. either this code gets executed before WordPress knows what template it's dealing with (trying to execute get_current_template(true) in there throws an "Undefined function" error)
  2. OR because $wp_query->is_404 is true by the time this code is run, it doesn't attempt to load the author.php template.

Pasting the code directly to the template didn't work either, even if it's just these three lines pasted before get_header();:

THE CODEEDIT: ATTEMPT 2

author.php I ditched (just the useful bit)template_redirect for hooking into pre_get_posts:

<?php iffunction (have_postsmy_post_queries() $query ) { ?>
   if <div(!is_admin() class="row&& archive$query-grid">>is_main_query()){
        <?php whileif (have_postsis_author()) : the_post();{
            include(locate_template('parts/loop-archive$query-grid.php')>set('posts_per_page', 3);
           // endwhile;NEW ?>CODE:
    </div>
<?php }

 $paged = (get_query_var('paged')) ? intval(get_query_var('paged')) : 1;
$secondloop = new WP_Query(array(
   global "post_type"$original_page_request; // saves the original value =>of "musikalbum",
$paged because I'm about "posts_per_page"to =>alter 3,it!
    "author_name"    =>if get_query_var('author_name'),
    "paged"          => $paged
));

if ($secondloop> $query->have_posts()>max_num_pages) { ?>
    <div class="row archive-grid">
        <?php while ($secondloop$query->have_posts>set()) :'paged', $secondloop$query->the_post(>max_num_pages);
            get_template_part( 'parts/loop',$original_page_request 'album-grid'= );$paged;
       } endwhile;else ?>{
    </div>
    <?php author_page_navi$query->set( $loop'posts_per_page', 9);
} else {  }
 }
}
add_action( 'pre_get_posts', 'my_post_queries' joints_page_navi();
} ?>

SettingThe idea was to manually set the main query page when it exceeds max_num_pages and store the "actual page" in posts_per_pageglobal $original_page_request for my main query in myeverything that needs it.

I then edited functionsauthor.php:

functionglobal my_post_queries$original_page_request;
// Check if max_num_pages is exceeded (= $queryif )$original_page_request {is set!)
// Also set the $paged variable for my $secondloop to the unaltered paged query var.
if (!is_adminempty($original_page_request)) &&{
 $query->is_main_query   $is_within_max = true;
    $paged = get_query_var('paged') ? intval(get_query_var('paged')) : 1;
} else {
    $is_within_max = false;
    $paged = $original_page_request;
}

// Main loop, hide it if max_num_pages exceeded!
if (is_author$is_within_max && have_posts()) { ?>
    <div class="row $queryarchive->setgrid">
        <?php while ('posts_per_page',have_posts()) 3: the_post();
    } else {
      $query->setinclude('posts_per_page', 9locate_template('parts/loop-archive-grid.php'));
    }
  }
}  endwhile; ?>
add_action( 'pre_get_posts', 'my_post_queries' ); </div>
<?php }

And here areProblem is, it doesn't work. I was so happy not to see a 404 any more, but now it automatically sets the pagination functions used from my JointsWP4 starter theme:main query page to 0.

<?php // This function was standard from my JointsWP4 starter theme...
function joints_page_navi($before = '', $after = '') {
  global $wpdb, $wp_query;
  $request = $wp_query->request;
  $posts_per_page = intval(get_query_var('posts_per_page'));
  $paged = intval(get_query_var('paged'));
  $numposts = $wp_query->found_posts;
  $max_page = $wp_query->max_num_pages;
  //...except for this line, since author_page_navi uses the same logic:
  page_navi_logic( $posts_per_page, $paged, $numposts, $max_page );
}

// A "hacked" version of joints_page_navi() for my $secondloop to work (or not)
function author_page_navi( $loop ) {
  global $wpdb, $wp_query;
  $request = $wp_query->request;
  $posts_per_page = max(intval($wp_query->posts_per_page), intval($loop->posts_per_page));
  $paged = intval(get_query_var("paged"));
  $numposts = max($wp_query->found_posts, $loop->found_posts);
  $max_page = max($wp_query->max_num_pages, $loop->max_num_pages);
  page_navi_logic( $posts_per_page, $paged, $numposts, $max_page );
}

function page_navi_logic( $posts_per_page, $paged, $numposts, $max_page ) {
  if ( $numposts <= $posts_per_page ) { return; }
  if(empty($paged) || $paged == 0) {
    $paged = 1;
  }
  $pages_to_show = 3;
  $pages_to_show_minus_1 = $pages_to_show-1;
  $half_page_start = floor($pages_to_show_minus_1/2);
  $half_page_end = ceil($pages_to_show_minus_1/2);
  $start_page = $paged - $half_page_start;
  if($start_page <= 0) {
    $start_page = 1;
  }
  $end_page = $paged + $half_page_end;
  if(($end_page - $start_page) != $pages_to_show_minus_1) {
    $end_page = $start_page + $pages_to_show_minus_1;
  }
  if($end_page > $max_page) {
    $start_page = $max_page - $pages_to_show_minus_1;
    $end_page = $max_page;
  }
  if($start_page <= 0) {
    $start_page = 1;
  }
  echo $before.'<nav class="push-down"><ul class="pagination">'."";
  if ($start_page >= 2 && $pages_to_show < $max_page) {
    $first_page_text = __( 'Erste...', 'jointswp' );
    echo '<li><a href="'.get_pagenum_link().'" title="'.$first_page_text.'">'.$first_page_text.'</a></li>';
  }
  echo '<li>';
  previous_posts_link( __('Back', 'jointswp') );
  echo '</li>';
  for($i = $start_page; $i  <= $end_page; $i++) {
    if($i == $paged) {
      echo '<li class="current"> '.$i.' </li>';
    } else {
      echo '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
    }
  }
  echo '<li>';
  next_posts_link( __('Forward', 'jointswp'), $max_page );
  echo '</li>';
  if ($end_page < $max_page) {
    $last_page_text = __( 'Last...', 'jointswp' );
    echo '<li><a href="'.get_pagenum_link($max_page).'" title="'.$last_page_text.'">'.$last_page_text.'</a></li>';
  }
  echo '</ul></nav>'.$after."";
}

0?!

When hooking into pre_get_posts, this is exactly what $query->max_num_pages gives me. That's because by that time, it's still in the process of gathering query variables and did not execute its query YET. So there are no posts to show because no posts retreived, and thus no max_num_pages.

I then copied the new code from my_post_queries( $query ) to another function which hooks into wp. There, I have access to max_num_pages and also the template I'm in. Still nothing. Because as soon as I exceed the number of posts, said code doesn't get executed. I could however set $wp_query->is_404 = false but that returns me to the index, yet again.

But it still shows me a 404 error. If I remove the if clause, then it leads me to the index.php which is not reeeally what I intended. My guess is that

  1. either this code gets executed before WordPress knows what template it's dealing with (trying to execute get_current_template(true) in there throws an "Undefined function" error)
  2. OR because $wp_query->is_404 is true by the time this code is run, it doesn't attempt to load the author.php template.

Pasting the code directly to the template didn't work either, even if it's just these three lines pasted before get_header();:

THE CODE:

author.php (just the useful bit)

<?php if (have_posts()) { ?>
    <div class="row archive-grid">
        <?php while (have_posts()) : the_post();
            include(locate_template('parts/loop-archive-grid.php'));
            endwhile; ?>
    </div>
<?php }

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$secondloop = new WP_Query(array(
    "post_type"      => "musikalbum",
    "posts_per_page" => 3,
    "author_name"    => get_query_var('author_name'),
    "paged"          => $paged
));

if ($secondloop->have_posts()) { ?>
    <div class="row archive-grid">
        <?php while ($secondloop->have_posts()) : $secondloop->the_post();
            get_template_part( 'parts/loop', 'album-grid' );
        endwhile; ?>
    </div>
    <?php author_page_navi( $loop );
} else {
    joints_page_navi();
} ?>

Setting the posts_per_page for my main query in my functions.php

function my_post_queries( $query ) {
  if (!is_admin() && $query->is_main_query()){
    if (is_author()){
      $query->set('posts_per_page', 3);
    } else {
      $query->set('posts_per_page', 9);
    }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

And here are the pagination functions used from my JointsWP4 starter theme:

<?php // This function was standard from my JointsWP4 starter theme...
function joints_page_navi($before = '', $after = '') {
  global $wpdb, $wp_query;
  $request = $wp_query->request;
  $posts_per_page = intval(get_query_var('posts_per_page'));
  $paged = intval(get_query_var('paged'));
  $numposts = $wp_query->found_posts;
  $max_page = $wp_query->max_num_pages;
  //...except for this line, since author_page_navi uses the same logic:
  page_navi_logic( $posts_per_page, $paged, $numposts, $max_page );
}

// A "hacked" version of joints_page_navi() for my $secondloop to work (or not)
function author_page_navi( $loop ) {
  global $wpdb, $wp_query;
  $request = $wp_query->request;
  $posts_per_page = max(intval($wp_query->posts_per_page), intval($loop->posts_per_page));
  $paged = intval(get_query_var("paged"));
  $numposts = max($wp_query->found_posts, $loop->found_posts);
  $max_page = max($wp_query->max_num_pages, $loop->max_num_pages);
  page_navi_logic( $posts_per_page, $paged, $numposts, $max_page );
}

function page_navi_logic( $posts_per_page, $paged, $numposts, $max_page ) {
  if ( $numposts <= $posts_per_page ) { return; }
  if(empty($paged) || $paged == 0) {
    $paged = 1;
  }
  $pages_to_show = 3;
  $pages_to_show_minus_1 = $pages_to_show-1;
  $half_page_start = floor($pages_to_show_minus_1/2);
  $half_page_end = ceil($pages_to_show_minus_1/2);
  $start_page = $paged - $half_page_start;
  if($start_page <= 0) {
    $start_page = 1;
  }
  $end_page = $paged + $half_page_end;
  if(($end_page - $start_page) != $pages_to_show_minus_1) {
    $end_page = $start_page + $pages_to_show_minus_1;
  }
  if($end_page > $max_page) {
    $start_page = $max_page - $pages_to_show_minus_1;
    $end_page = $max_page;
  }
  if($start_page <= 0) {
    $start_page = 1;
  }
  echo $before.'<nav class="push-down"><ul class="pagination">'."";
  if ($start_page >= 2 && $pages_to_show < $max_page) {
    $first_page_text = __( 'Erste...', 'jointswp' );
    echo '<li><a href="'.get_pagenum_link().'" title="'.$first_page_text.'">'.$first_page_text.'</a></li>';
  }
  echo '<li>';
  previous_posts_link( __('Back', 'jointswp') );
  echo '</li>';
  for($i = $start_page; $i  <= $end_page; $i++) {
    if($i == $paged) {
      echo '<li class="current"> '.$i.' </li>';
    } else {
      echo '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
    }
  }
  echo '<li>';
  next_posts_link( __('Forward', 'jointswp'), $max_page );
  echo '</li>';
  if ($end_page < $max_page) {
    $last_page_text = __( 'Last...', 'jointswp' );
    echo '<li><a href="'.get_pagenum_link($max_page).'" title="'.$last_page_text.'">'.$last_page_text.'</a></li>';
  }
  echo '</ul></nav>'.$after."";
}

THE CODE:

author.php (just the useful bit)

<?php if (have_posts()) { ?>
    <div class="row archive-grid">
        <?php while (have_posts()) : the_post();
            include(locate_template('parts/loop-archive-grid.php'));
            endwhile; ?>
    </div>
<?php }

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$secondloop = new WP_Query(array(
    "post_type"      => "musikalbum",
    "posts_per_page" => 3,
    "author_name"    => get_query_var('author_name'),
    "paged"          => $paged
));

if ($secondloop->have_posts()) { ?>
    <div class="row archive-grid">
        <?php while ($secondloop->have_posts()) : $secondloop->the_post();
            get_template_part( 'parts/loop', 'album-grid' );
        endwhile; ?>
    </div>
    <?php author_page_navi( $loop );
} else {
    joints_page_navi();
} ?>

Setting the posts_per_page for my main query in my functions.php

function my_post_queries( $query ) {
  if (!is_admin() && $query->is_main_query()){
    if (is_author()){
      $query->set('posts_per_page', 3);
    } else {
      $query->set('posts_per_page', 9);
    }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

But it still shows me a 404 error. If I remove the if clause, then it leads me to the index.php which is not reeeally what I intended. So I need that if clause...but WordPress doesn't seem to know it yet!

Pasting the code directly to the template didn't work either, even if it's just these three lines before get_header();:

EDIT: ATTEMPT 2

I ditched template_redirect for hooking into pre_get_posts:

function my_post_queries( $query ) {
  if (!is_admin() && $query->is_main_query()){
    if (is_author()){
      $query->set('posts_per_page', 3);
      // NEW CODE:
      $paged = (get_query_var('paged')) ? intval(get_query_var('paged')) : 1;
      global $original_page_request; // saves the original value of $paged because I'm about to alter it!
      if ($paged > $query->max_num_pages) {
        $query->set('paged', $query->max_num_pages);
        $original_page_request = $paged;
    } else {
      $query->set('posts_per_page', 9);
    }
 }
}
add_action( 'pre_get_posts', 'my_post_queries' );

The idea was to manually set the main query page when it exceeds max_num_pages and store the "actual page" in global $original_page_request for everything that needs it.

I then edited author.php:

global $original_page_request;
// Check if max_num_pages is exceeded (= if $original_page_request is set!)
// Also set the $paged variable for my $secondloop to the unaltered paged query var.
if (empty($original_page_request)) {
    $is_within_max = true;
    $paged = get_query_var('paged') ? intval(get_query_var('paged')) : 1;
} else {
    $is_within_max = false;
    $paged = $original_page_request;
}

// Main loop, hide it if max_num_pages exceeded!
if ($is_within_max && have_posts()) { ?>
    <div class="row archive-grid">
        <?php while (have_posts()) : the_post();
            include(locate_template('parts/loop-archive-grid.php'));
        endwhile; ?>
    </div>
<?php }

Problem is, it doesn't work. I was so happy not to see a 404 any more, but now it automatically sets the main query page to 0.

0?!

When hooking into pre_get_posts, this is exactly what $query->max_num_pages gives me. That's because by that time, it's still in the process of gathering query variables and did not execute its query YET. So there are no posts to show because no posts retreived, and thus no max_num_pages.

I then copied the new code from my_post_queries( $query ) to another function which hooks into wp. There, I have access to max_num_pages and also the template I'm in. Still nothing. Because as soon as I exceed the number of posts, said code doesn't get executed. I could however set $wp_query->is_404 = false but that returns me to the index, yet again.

deleted 628 characters in body
Source Link

I could now:

  1. figure out how to make this above attempt work, specifying a working if clause (because I don't want to redirect 404s everywhere and especially not to the index every time..). Is that possible? Especially, how can I remain inside my author.php template even if $wp_query->is_404 is true?
  2. make 2 split paginations for both my main query and my $secondloop query and then altering my pagination function
  3. fiddle with AJAX (and the WP REST API, I assume?), which is not what I intended to do for a quick fix so I'd rather avoid it if the aforementioned solutions are quicker to implement.

I could now:

  1. figure out how to make this above attempt work, specifying a working if clause (because I don't want to redirect 404s everywhere and especially not to the index every time..). Is that possible? Especially, how can I remain inside my author.php template even if $wp_query->is_404 is true?
  2. make 2 split paginations for both my main query and my $secondloop query and then altering my pagination function
  3. fiddle with AJAX (and the WP REST API, I assume?), which is not what I intended to do for a quick fix so I'd rather avoid it if the aforementioned solutions are quicker to implement.
adding some more thoughts to Edit 1
Source Link
Loading
added 312 characters in body
Source Link
Loading
attempt at fixing failed, added that into EDIT 1
Source Link
Loading
attempt at fixing failed, added that into EDIT 1
Source Link
Loading
Added code as requested
Source Link
Loading
stripped unnecessary info and refined wording
Source Link
Loading
deleted 10 characters in body
Source Link
Loading
deleted 64 characters in body; added 1 character in body
Source Link
Loading
deleted 78 characters in body; deleted 46 characters in body; deleted 3 characters in body
Source Link
Loading
edited tags; deleted 4 characters in body; deleted 1 character in body; added 5 characters in body
Source Link
Loading
Source Link
Loading