0

Currently, I'm trying to display all posts from an author(e.x admin) in a custom page with name that is completely different than these template file listed here.

So inside this page, I'm using WP query to get posts from admin like this:

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 10,
    'post_status' => 'publish',
    'author_name' => 'admin'
);

$author_query = new WP_Query( $args );

It's working fine with 10 posts received, but the problem is with the pagination since I cannot get the correct max_num_pages inside my pagination function:

global $wp_query;
echo $pages = $wp_query->max_num_pages; // Always return 0

When I do print_r($wp_query); it's always showing [max_num_pages] => 0 and [found_posts] => 1 where as the exact same code in author.php gave me [max_num_pages] => 1 and [found_posts] => 5.

So do I need to pass any other specific parameters to the WP Query or any other way to overcome this problem?

1

From the info you have given in your post, I believe you are using a custom page.php template here

Here is the the reasons you get the output as stated:

The main query executes on each and every page that is loaded. The main query is very specific for every type of template. $wp_query is the super global used by the main query

To test the how unique the main query behaves on all the specific templates, add print_r($wp_query); to all archive pages (archive.php, category.php, author.php etc), index.php and page.php. As you can see, the results are quite different for each instance. To understand how that works, you have to read Query Overview in the codex

OK to come back, the reason why you get the specific output when you use $wp_query, it displays info from the main query for the page, not info from your custom query.

To get info from your custom query, you have to print_r(VARIABLE USED FOR new WP_Query);, in your case print_r($author_query).

To get pagination to work on your custom query, you have the pass the paged parameter to your arguments. Also, when using next_posts_link(), you have to specify the $max_pages parameter

Here is a working example from the codex. Modify as needed to suite your arguments etc.

<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// the query
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 10,
    'post_status' => 'publish',
    'author_name' => 'admin'
    'paged' => $paged
);

$author_query = new WP_Query( $args ); 
?>

<?php if ( $author_query->have_posts() ) : ?>

<?php
// the loop
while ( $author_query->have_posts() ) : $author_query->the_post(); 
?>
<?php the_title(); ?>
<?php endwhile; ?>

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $author_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>

<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

EDIT

This post might also share some more light on the main query and how it works

EDIT 2

I have updated the code to show your specific from your question. Hope this is helpful

  • Well, thank you very much for your comprehensive answer. All these answers are really awesome which helps me to understand better about pagination as well as Wordpress in common. Sadly I cannot give all three answers a check mark so I choose your answer since you're putting most efforts to help me out here :P but anyway thank you very much again for all of yours help – user3705773 Sep 4 '14 at 18:14
  • Glad I could be of help. Enjoy :-) – Pieter Goosen Sep 4 '14 at 18:26
1

Because the global $wp_query represents the query for the current page (the "main" query) and is completely separate from your custom $author_query.

If you want to use pagination functions, you can "fake it" by temporarily setting $wp_query to your custom query, and then resetting afterwards:

global $wp_query, $wp_the_query;

$wp_query = $author_query;

// Pagination functions

$wp_query = $wp_the_query;
  • Thank you very much for your answer since it shed me some light! Sadly I'm unable to mark more than 1 correct answer. Also, I cannot vote you up using this account but I'll create another account with free 100 points from stackexchange to vote you up :P – user3705773 Sep 4 '14 at 18:13
1

Here is the query format for displaying author posts with pagination. You will have to also add paged argument to paginate.

<?php

    // fix pagination issue
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }

    $args = array(
      'post_type' => 'post',
      'posts_per_page' => '10',
      'author_name' => 'admin',
      'ignore_sticky_posts' => 1,
      'paged' => $paged
    );

    $my_query = new WP_Query( $args );

    if ( $my_query->have_posts() ) :

      while ( $my_query->have_posts() ) : $my_query->the_post();

        get_template_part( 'content', get_post_format() );

      endwhile;

      twentytwelve_content_nav( 'nav-below' );

    else :

        get_template_part( 'content', 'none' );

    endif;

    wp_reset_postdata();

?>

In this code I am assuming you are using twentytwelve_content_nav() function to produce prev, next links. Although you can change it to replace your code.

  • Not sure what is $paged you've mentioned here but the problem with me is that the returned data from global WP_Query inside my custom page which I retrieved from pagination function is not the same when I use this same pagination function in author.php – user3705773 Sep 4 '14 at 17:43
  • Try this code now. I have change it so it should work now. Test it and let me know if it still does not work. – Robert hue Sep 4 '14 at 17:59
  • Thank you very much for your efforts since it helps me to understand better about Wordpress! Sadly I'm unable to mark more than 1 correct answer. Also, I cannot vote you up using this account but I'll create another account with free 100 points from stackexchange to vote you up since you guys are really deserve it. – user3705773 Sep 4 '14 at 18:14

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.