Wordpress Pagination with custom query loads same content on every page - php

I am using 2 custom queries to load posts on a custom Wordpress template homepage.
the first query loads 4 'sticky' posts, and the next query loads several more recent posts.
This works great and does what I need it to do, but the pagination links when clicked show the same content on every page.
For example I have 10 posts on the homepage, and when i click to page 2 - the exact same 10 posts appear again. If I click pagination to visit page 3, the same 10 posts again etc.
No matter what page I am on, the same 10 posts are listed.
I have read something about paged being required but several failed attempts to include that in my below code have broguht me here.
Any ideas appreciated!
My code...
<?php $sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<main id="main" class="site-main" role="main">
<?php $the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>

You sure need the $paged parameter (docs). Untested but give it a go:
<?php
// $paged holds the current page offset
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
/**
* - Checks if the current page is 'paged' (false on first page)
* - Remove the check if you need those sticky posts on all pages
* - I added the $paged parameter so those sticky posts will paginate
* if you decide to show them on all pages
*
*/
if( ! is_paged() ) { // Sticky posts only on first page
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
'paged' => $paged // ah, the page offset
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<?php the_title(); ?>">
<?php
}
} else {
?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php
}
wp_reset_postdata();
}
?>
<main id="main" class="site-main" role="main">
<?php
$args = array(
'post__not_in' => get_option( 'sticky_posts' ),
'paged' => $paged // ah, the page offset
);
// can be done via new WP_Query but today I am lazy
// also see http://codex.wordpress.org/Function_Reference/query_posts
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
get_template_part( 'content', get_post_format() );
}
} else {
get_template_part( 'content', 'none' );
}
wp_reset_query();
?>

Related

Iterating over correct number of wordpress posts from category + custom post type

Our wordpress post loop combines and displays posts from a specific category and a custom post type. This works, but is not displaying all posts. I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type. How can I ensure that the correct number of posts are being displayed?
<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
<?php the_post_thumbnail( 'full' ); ?>
<?php the_content(); ?>
<?php if ( $cats = get_field( 'category' ) ) : ?>
<?php
$args = array(
'post_type' => array( 'post' ),
'category__in' => $cats,
'fields' => 'ids',
);
$articles = new WP_Query( $args );
wp_reset_postdata();
$args = array(
'post_type' => array( 'case_study' ),
'fields' => 'ids',
);
$case_study = new WP_Query( $args );
wp_reset_postdata();
$all_posts_ids = array_merge( $articles->posts, $case_study->posts );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'case_study' ),
'post__in' => $all_posts_ids,
'paged' => $paged,
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'blocks/pager' ); ?>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>
There is a number of things you're doing wrong.
You are using a page template, and then are looping using the loop.
This probably pulls all posts, as the last fallback for page template is index.php (as seen in the diagram here).
Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries).
And the last query, you are using query_posts() which is overriding the main query. Just don't ever use that.
So the plan of attack should be:
What do I want to show?
How and where do I want to show it?
What do I need to do to achieve this?
Start writing things needed to achieve this.
???
Profit!!!
If you want to control specific taxonomy, use $taxonomy.php template (with $taxonomy being the name of the taxonomy.).
Hope this helps.

why page 2 not working in wp query

Why http://localhost/wp/?paged=2 and older not working in wp_query?
I have movie and series from "custom post type" more than 15 posts.
It's separated in 2 pages in the front-end, but when I press the older post, the page is still first page.
Here is my index.php loop code:
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => array('post','series','movie'),
'posts_per_page' => 10,
'paged' => $paged
);
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1><?php the_title();?></h1>
<?php endwhile; ?>
<div class="col-md-12">
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
I've found a solution:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( $query->is_main_query() && is_home() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_category() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_search() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_tag() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
return $query;
}
this code isn't stop appearing navigation menu

Implementing pagination in wp doesn't work

I'm trying to add some code to my wordpress theme to show a pagination at the bottom of the posts.
Here's my loop with the pagination:
<main id="main">
<?php
// the query
$args = array('posts_per_page' => 2 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<!-- loop -->
<?php while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article id="post">
<div id="thumbnail">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(); } ?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</article>
<?php } } else { ?>
<p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
<?php } ?>
<!-- pagination -->
<?php
if($the_query->max_num_pages>1){?>
<p class="paged">
<?php
if ($paged > 1) { ?>
<
<?php }
for($i=1;$i<=$the_query->max_num_pages;$i++){?>
<a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
<?php
}
if($paged < $the_query->max_num_pages){?>
>
<?php } ?>
</p>
<?php } ?>
<!-- end pagination -->
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</main>
When I'm looking through the source code I can't find the pagination. What am I doing wrong? Can't find an answer, no code works for me. Would be nice if someone could help me. :)
Use default WordPress pagination, here is an example:
<?php
// set the "paged" parameter
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
// Pagination
echo get_next_posts_link( 'Older', $the_query->max_num_pages );
echo get_previous_posts_link( 'Newer' );
// clean up after our query
wp_reset_postdata();
else:
_e( 'Sorry, no posts matched your criteria.' );
endif;
To display next and previous links automatically, just use these functions:
next_posts_link();
previous_posts_link();
https://codex.wordpress.org/Pagination could help with this. In custom loops it helps to pass in the $max_pages variable as zero for unlimited pages, so it'd look like:
next_posts_link("Older Posts", 0);
If you want the pagination to include page links, in the format < 1 2 3 > rather than just next/previous links, you can use the WordPress function paginate_links.
So for your example the code would look like this:
<?php
//query
$paged = (isset($_REQUEST['paged']) && $_REQUEST['paged'] > 0 ? $_REQUEST['paged'] : max( 1, get_query_var('paged') ));
$args = array(
'posts_per_page' => 2,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//display your post here
}
//pagination
if($the_query->max_num_pages > 1){ ?>
<div class="paged">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $the_query->max_num_pages,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
) );
?>
</div>
<?php }
} else {
echo '<p>'._e( 'Die Posts entsprechen nicht den Kriterien.' ).'</p>';
}
wp_reset_postdata();
?>

Wordpress Static frontpage search pagination

I have a static frontpage on my wordpress website, displaying my posts. Currently it shows 10 posts and I want to make it possible to click "next page" to see the next 10 posts in the query and so on. I have tried getting the pagination to work, but it looks like I'm unable to connect it with my search query.
Here is my query:
<?php
query_posts(
array( 'post_type' => 'post',
'order' => 'DESC',
'meta_key' => 'cf_votes',
'orderby' => 'meta_value_num',
'posts_per_page' => 10
)
);
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
And here is the pagination I have tried:
<?php
global $wp_query;
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
So basically I'm able to see the first 10 posts perfectly. But no alternative to see more posts is shown.
Wordpress Codex is pretty comprehensive on the Pagination problem, I recommend you to see this:
http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
There's a section specifically for the Static Front Page.
Something like:
<?php
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(
'posts_per_page' => 3,
'paged' => $paged
);
query_posts($args);
?>
<?php if ( have_posts() ) : ?>
<!-- Add the pagination functions here. -->
<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post(); ?>
<!-- the rest of your theme's main loop -->
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Wordpress - Limiting Number of Posts

How can I adapt this code to limit the number of posts to say 3 on the page? It's taken from a Wordpress template. Thanks in advance for any help!
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$alt_args = array(
'ignore_sticky_posts' => 1,
'paged' => $paged
);
$alt_posts = new WP_Query($alt_args);
?>
<?php if ( $alt_posts->have_posts() ) : ?>
<?php while ( $alt_posts->have_posts() ) : $alt_posts->the_post(); ?>
<?php get_template_part( 'content', 'alt-homepage' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'wp-jurist' ); ?></h1>
set the posts_per_page parameter:
$alt_args = array(
'ignore_sticky_posts' => 1,
'paged' => $paged,
'posts_per_page' => 3
);
see http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters for details
While is not the right thing to do, this will work:
<?php
$i = 0;
while ( $alt_posts->have_posts() && $i < 3 ) {
$i++;
$alt_posts->the_post();
get_template_part( 'content', 'alt-homepage' );
} ?>
The code above will limit the maximum of proccessed elements to 3. Just change the while part, and it'll run. Anyway, the correctness would be that the have_post() method would have an option to limit the number of elements retrieved, and then only show that elements.

Categories