Wordpress pagination, wp_query not working on page 2 - php

I have a problem with WP Pagination.
Previously, my pagination worked on taxonomy pages and was displaying same result on page 2 than page 1 on Static Homepage.
I found a solution to this problem, and 3 days ago all my page worked perfectly.
Until yesterday : now pagination still works on taxonomy pages, but on my static Homepage, page 1 works, but page 2 and more return "No article in this category" like if wp_query loop don't find results.
No file has been edited, functions are the same than 3 days ago.
PS : If I remove correction of first bug (Page 2 shows the same than page 1), the bug don't come back, homepage page 2 still returns "No article in this category";
Here is my code :
// Define $post_type in previous included file, already checked if parameter correctly retrieved
$post_type = array('magicrecipe', 'post', 'yoga-article', 'feelgood', 'gastronomie', 'voyage');
<ul class="articlesList row mw">
<?php
$taxQuery = array('taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $categoryValue);
// "Page 2 show the same than page 1" fix
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
if ($search) {
$args = array(
'posts_per_page' => 12,
'paged'=>$paged,
'orderby'=> 'date'
);
$args['s'] = $search;
}else{
$args = array(
'post_type' => $post_type,
'posts_per_page' => 12,
'orderby'=> 'date',
'paged'=> $paged,
'tax_query' => array(array($taxQuery))
);
}
$loop = new WP_Query($args);
$count = $loop->post_count;
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $loop;
if ($count == 0) {
echo '<p class="articles-noArticle">Il n\'y a pas encore d\'article dans cette catégorie !</p>';
} else {
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<li class="articlesList articles-article col-1-4 col-m-1-2 col-s-1-1">
<a href="articles-link">
<div class="articles-thumbContainer js-squareThumb">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(array(500, 500), array('class' => 'articles-thumb')); ?>
</a>
</div>
<h3 class="article-title"><?php the_title(); ?></h3>
<p class="article-excerpt"><?php the_excerpt(); ?></p>
En savoir plus
</a>
</li>
<?php
endwhile;
}
</ul>
<div class="pagination">
<?php
// Custom query loop pagination
echo paginate_links();
// wp_reset_query();
?>
</div>
Thanks for your help, I search everywhere but found anything about this problem.

You have too many conditions.
<?php
//You don't need that
/*if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }*/
//Try this:
<?php
wp_reset_query();
$paged = get_query_var('paged', 1);
$args = array(
'post_type' => $post_type,
'posts_per_page' => '12',
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'post-date'
);
$loop = new WP_Query( $args);
?>

Related

Tag template instead of 'post' in the following loop

I'm using the code (loop) below for the blog post grid displaying. I want the following structure for my tag.php as well (the post by the tag instead of all blog posts). But I don't know how to do it. Could you, please, check it?
I can't remember how I solved the following problem in the past but I'm at a 100% sure that I found the solution previously.
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="item col-sm-4">
<div class="well">
<h2><?php the_title(); ?></h2>
<span class="date"><?php echo get_the_date("j.n.Y"); ?></span>
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Suite</a>
</div>
</div></div>
<?php endwhile; ?>
</div>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="next-posts-link">
<?php echo get_previous_posts_link( '< Page précédente' ); ?>
</div>
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Page suivante >', $custom_query->max_num_pages ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Please check WP_Query with Tag Parameter
https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
example:
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date', // modified | title | name | ID | rand
'tag' => 'tag1,tag2'
);

Show Author post loops

So, I have the following to display post loops (wordpress):
METHOD A (works fine)
<?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(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts go here
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<nav id="rh_nav_below">
<ul>
<li class="rh_nav_previous"><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li class="rh_nav_next"><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
Now in the author page, following is used to display a post (a single posts):
METHOD B (works fine)
<?php rewind_posts(); while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
I have been trying to change the author post (method b) to method A format, so I can control the number of posts, orderby and etc.
Here is what I have tried:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$rhp_author_profile_id = get_the_author_id();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $rhp_author_profile_id
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts show here.
However, I am only getting the admin's posts on every other authors.
What am I doing wrong?
Thanks
Try This
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
global $current_user;
get_currentuserinfo();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $current_user->ID
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>

Showposts 30 but 10 per page

I have now this piece of code to show on each page 10 posts but know I want still the same but the maximum of posts in total can only be 30.
How do I do this?
<?php query_posts('showposts=10&paged='.$paged);?>
This is code where you want to display the post:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 10,'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post); ?>
<h1> <?php the_post_thumbnail(); ?></h1>
<h2><?php the_title(); ?></h2>
<h3><?php the_content(); ?></h3>
<?php endwhile; ?>
This is footer where you need to display pagination.
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if($loop->max_num_pages>1){?>
<ul class="pager">
<?php for($i=1;$i<=$loop->max_num_pages; $i++){ ?>
<li> <?php echo $i; ?></li>
<?php } ?>
</ul>
You should use wp_query to retrieve the posts, like Pieter Goosen said, is more efficient. Also wp_query have a property called $max_num_page that you can use to limit the number of pages you get.
You can modify your pagination to something like this:
global $wp_query;
if ( $wp_query->max_num_pages > 1 ){
$current_page = max( 1, get_query_var('paged') );
$max_pages = 3;
$args = array(
'base' => #add_query_arg('paged','%#%'),
'format' => '/paged/%#%',
'current' => $current_page,
'total' => $max_pages,
'show_all' => false,
'type' => 'array',
'paged' => 1
);
$pages = paginate_links( $args );
if (is_array($pages)) {
$paged = ( get_query_var('paged') == 0) ? 1 : get_query_var('paged');
foreach( $pages as $page) {
echo "$page";
}
}
}
I have also found this question. This code seems to work and you don't need to change any of your current code:
add_filter('pre_get_posts', 'limit_pages');
function limit_pages($query) {
$query->max_num_pages = 3;
if ($query->query_vars['paged'] > 3) {
$query->query_vars['paged'] = 3;
$query->query['paged'] = 3;
}
return $query;
}

Wordpress pagination links reload page

I am trying to build a paginated archive sidebar using WP_Query. As the same sidebar is used on both single.php and news.php, the arguments used for the WP_Query are different.
On news.php, the pagination works perfectly, but on single.php the pagination links are present, and have the correct href values, but simply reload the page when clicked, i.e. anything beyond page 1 is inaccessible.
Update: Just in case this is relevant, single.php makes use of the Share Buttons by AddToAny, and Social (by MailChimp) plugins. I have deactivated both, but this has not fixed the issue with pagination.
Can anyone shed some light on this for me? Code here:
<aside>
<header>
<h1 class="column-title">Archive</h1>
<hr>
</header>
<div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// remove first four posts if this is the news main page
if ( ! is_single() ) {
// Save first four posts
$first_four = new WP_Query ('post_type=post&orderby=date&order=desc&posts_per_page=4');
if ( $first_four->have_posts() ) : while ( $first_four->have_posts() ) : $first_four->the_post();
$skipIDs[] = $post->ID;
endwhile; endif;
wp_reset_postdata();
// Save all posts
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1
);
$all_posts = new WP_Query($args);
while ( $all_posts->have_posts() ) : $all_posts->the_post();
// Skip first four posts and save rest
if ( in_array($post->ID,$skipIDs) ) { continue; };
$offset_array[] = $post->ID;
endwhile;
wp_reset_postdata();
// Final arguments for WP_Query
$args = array(
'post__in' => $offset_array,
'paged' => $paged,
'posts_per_page' => 5
);
} else {
// Args for single.php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
'paged' => $paged
);
}
$article_archive = new WP_Query($args);
$max_pages = $article_archive->max_num_pages;
if( $article_archive->have_posts() ) : while( $article_archive->have_posts() ) : $article_archive->the_post();
$has_image = get_the_post_thumbnail($post->ID,'thumbnail'); ?>
<article class="group">
<a class="anchor-overlay" href="<?php the_permalink(); ?>"></a>
<?php if( $has_image ) { echo $has_image; } else { echo "<img src='/wp-content/themes/DCSFC/images/news-calendar/news/no-image.jpg' alt='No image' />"; } ?>
<div>
<h3><?php the_title(); ?></h3>
<time datetime="dd/MM/YYYY"><?php echo get_the_date(); ?></time>
</div>
</article>
<?php endwhile; endif;
wp_reset_postdata();
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $max_pages
) );
?>
</div>

Trying to display posts only from one category in wordpress

I am using the magnet theme and am trying to display posts from a single category on the homepage. I have read through several post but have had no luck using the suggested methods I found.
Here is the snippet of code from the home-page-grid.php file that appears to be adding posts to the homepage
<!-- Start content -->
<div class="grid_8" id="content">
<div class="widget_container content_page">
<div class="post_list_medium_widget">
<div class="post_list_medium_style1">
<?php
global $paged;
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$query = new WP_Query( array ( 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' ) );
$row_count=0;
while ( $query->have_posts() ) {
$row_count++;
$query->the_post();
$post_id = get_the_ID();
?>
Any thoughts as to what needs to be done to get this to display a single category and all its sub categories?
Try adding this array item:
'cat' => '14' . 14 is the category id
$query = new WP_Query( array ( 'paged' => $paged, 'cat' => '14', 'orderby' => 'date', 'order' => 'DESC' ) );
<?php
$catPost = get_posts(get_cat_ID("your_category_name")); //change this with your category
foreach ($catPost as $post) : setup_postdata($post); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php endforeach;?>

Categories