Why is next_post_link() not working with WP_Query? - php

Been researching a pagination issue I am having with WordPress. My page-foobar.php file is:
<?php
// the query
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'ignore_sticky_posts' => true, );
$foo_query = new WP_Query( $foobar_args );
if ( $foo_query->have_posts() ) : ?>
<?php while ( $foo_query->have_posts() ) : $foo_query->the_post();?>
// code
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
I added:
<nav>
<?php previous_posts_link('« Newer',$foo_query->max_num_pages); ?>
<?php next_posts_link('Older »',$foo_query->max_num_pages); ?>
</nav>
after referencing this answer after the endif;:
The link shows in the url as site/foobar/page/2/ but displays the 404 page. After some further research I ran across an article that mentioned the page-foobar.php and posttype-foobar.php names can clash so to rename one of them. I renamed posttype-foobar2.php, changed my functions.php, tested and still get sent to the 404 page with the correct URL. Another article mentioned that you must use wp_query() so I changed my page-foobar.php to:
<?php
// the query
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'ignore_sticky_posts' => true, );
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
// code
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
pagination:
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
I did test this by adding this before <?php wp_reset_postdata(); ?> and after <?php endwhile; ?>:
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
tested and I still get sent to a 404 page. When I change next_posts_link() to <?php var_dump(next_posts_link('« Older Entries', $wp_query->max_num_pages)); ?> it returns NULL. If I change next_posts_link() to get_next_posts_link() I get string(99). What am I doing wrong with my pagination?
Further Referenced:
Wordpress pagination (next_posts_link) on custom wp_query not showing
next_posts_link 404 error
WP_Query and next_posts_link
How can I properly get my pagination to go to the next page?

// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php the_title(); echo "<br/>"; ?>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>

After doing some further research I ran across the solution to my problem and I hope it can help someone else. The answer submitted by Sindhu does have merit in respect to the addition of:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
and the modification to the arguments:
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
Codex for get_query_var(). After making this additional change it still didn't work so I started testing my Permalinks. When I ran the code under Default the pagination worked. When I changed to Post name and saved twice it still didn't work. After browsing I decided to search for custom post type giving 404 error and I ran across "Fixing Custom Post Type 404 Errors In WordPress" which mentioned I could be creating a double slug. After review I was in fact doing so and WordPress was clashing with the double slug. I decided to changed my posttype-foobar.php to posttype-foobar_t.php and updated my functions.php file. Instead of modifying the database on my localhost to fix the slug issue I decided to install a clean installation with the modified changes and it works.
I would also like to point out some of my findings in regards to a large majority of posts mentioned you have to call the query as $wp_query = new WP_Query( $foobar_args );.

Related

next_posts_link() won't return anything when using pagination with a WP posts query

I've poured over dozens of posts, and the documentation, nothing has helped.
I have a very simple problem: I want to query for all posts of some category, then split up the results into pages. I have tried a few different things, but essentially the next_posts_link() and get_next_posts_link() never returns anything.
Latest code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged,
'cat' => 135
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="med-item talks-texts">
<p class="bold"><?php the_title(); ?> (<?php the_time('Y'); ?>)</p>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>
<?php next_posts_link(); ?>
Add parameters to the function. In your case:
<?php echo get_next_posts_link('Older', $query->max_num_pages);?>
<?php echo get_previous_posts_link('Newer', $query->max_num_pages);?>
It looks to me as if the next_post_link() needs to be within the loop. You have it outside. There's more on this exact use-case in the WP docs for that function here:
https://developer.wordpress.org/reference/functions/next_posts_link/#div-comment-1297
Note the extra parameter needed for a custom WP_Query, as that's what you're running here. In short:
// next_posts_link() usage with max_num_pages.
next_posts_link( __( 'Older Entries', 'textdomain' ), $the_query->max_num_pages );

How to get post using a specific category

I'm just getting back into Wordpress and php after a few years away. I'm pretty ignorant when it comes to php syntax so making lots of mistakes.
I need to display a single post on a page, it's title and contents, and it needs to be specific to a category that I've created.
I grabbed this of the web and tried to change it, but I can't get it to work.
<?php query_posts('category_name=qaadrant&showposts=1');
while (have_posts()) : the_post();
// do whatever you want
?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
Any help much appreciate. Niall
Please use awesome WP_QUERY Class see the reference here:
https://codex.wordpress.org/Class_Reference/WP_Query
Using following code to get posts from category you selected. No-paging is set to make query faster:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category_name' => 'qaadrant',
'no-paging' => true,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php echo get_the_title(); ?></h3>
<p><?php echo get_the_excerpt(); ?></p>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

Wordpress Custom Loop doesn't show any posts

I'm trying to add a custom loop to display post titles from the category homepage. Here's the code I have so far so, but it's not displaying any posts. BTW, I'm using this code in single.php.
<?php $recentPosts = new WP_Query(); ?>
<?php if ( $recentPosts->have_posts() ) : ?>
<?php $recentPosts->query_posts( array ( 'category_name' => 'homepage', 'posts_per_page' => 10 ) ); ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I'm not too sure what's going on, but would appreciate any help I can get.
You're doing a lot of unnecessary messing around with your query object. Since you're dealing with an archive page, you should be able to create a new template file called category-homepage.php (assuming the slug of the category is homepage). In it, you could place something like the following:
$args = array(
'category_name' => 'homepage',
'posts_per_page' => 10
);
$recentPosts = new WP_Query( $args );
if ( $recentPosts->have_posts() ) :
while ( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
Again, since it's a category archive page, you shouldn't be doing anything to single.php. You can read more about template hierarchy in the Codex.

Wordpress Query by Tag(Priority) then Category(backfill)

I've search high and low but have been unable to find a solution to my problem. Hopefully this isn't a duplicate question that I was unable to find through search here and on google.
I'm attempting to have a wp_query return a set number of results (10) that are populated by any posts found in the current pages category. I was able to accomplish this with...
$postCategories = get_the_category();
$atts = array (
'posts_per_page' => 10,
'tag' => 'sticky',
'category_name' => $postCategories[0]->slug,
);
But where I'm having trouble is with the tag. I would like any posts that have the tag 'sticky' to take priority over any of the posts being brought in by the category match while not adding any more than 10 results still.
Any help or guidance would be much appreciated as i'm kind of a newbie to php. Thanks
I think this could work for you, but it's hard to know for sure without knowing the specifics of your project.
<ul>
<?php $sticky = get_option( 'sticky_posts' ); // Get sticky posts ?>
<?php $args_sticky = array(
'post__in' => $sticky,
'posts_per_page' => 10, // Limit to 10 posts
'ignore_sticky_posts' => 1
); ?>
<?php $sticky_query = new WP_Query( $args_sticky ); ?>
<?php $sticky_count = count($sticky); // Set variable to the number of sticky posts found ?>
<?php $remaining_posts = 10 - count($sticky); // Determine how many more non-sticky posts you should retrieve ?>
<?php if ($sticky_count > 0) : // If there are any sticky posts display them ?>
<?php while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php if ($remaining_posts > 0) : // If there are non-sticky posts to be displayed loop through them ?>
<?php $postCategories = get_the_category(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => $remaining_posts,
'post__not_in' => get_option( 'sticky_posts' ),
'category_name' => $postCategories[0]->slug
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php endif; ?>
</ul>

WordPress Category Pagination not working

I have two custom post types from the archive.php One is archive-slug.php and the other is category-slug.php. The pagination works on the archive-slug.php but the same code on the category-slug.php won't even show. I'm somewhat new to Wordpress and php so i'm sure i'm missing something here, I just don't know what?
<?php
// Custom Post Type
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'available_dogs',
'category_name' => 'adopted-dogs',
'posts_per_page'=> 9,
'paged'=> $paged
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="dog-info-box col-lg-4 col-sm-6 col-xs-12">...</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
<?php
// Get the pagination
fusion_pagination( $pages = '', $range = 2 );
?>
<?php if( $sidebar_exists == true ): ?>
<?php wp_reset_query(); ?>
As I want to inform you that when you create a file "category-slug.php : it's an default template.No need to add the query posts.
It will display the posts of the category whose slug is added in file like category-slug.php.Here is the modifying code.
if (have_posts() ) :while ( have_posts() ) : the_post();
the_content();//content is going on.
endwhile;//pagination functionfusion_pagination( $pages = '', $range = 2 );else : _e( 'Sorry, no posts matched your criteria.' ); endif;
Precaution :1:Pagination code should be placed just before of the while loop.2:In your code pagination code have been place after the endif that's why it is not working.
Thanking you.
I used this code and got it working. It's not pretty but it will do for now. For what ever reason when I try to use it after the loop it doesn't work. When I use it after the endif it works.
<?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 } ?>
Please follow this steps.
1: Please make sure, your category have the posts.
2: If your category slug is "test" then your file name should be " category-test.php . if any confusion then please let me know.
3: See the code and write in file.
if(have_posts()) :while(have_posts()): the_post();the_title();the_content();endwhile;//pagination of wp
previous_posts_link( '« Newer Entries' );
next_posts_link( 'Older Entries »', 0 );endif;
Note:1:Please place this code in file and run if it will running successfully, then we will add the designing part.
2: If you can share the website URL then I can also give suitable solution because then I can seen the category slug etc .
Please let me know if still needs problem.
Thanking.
I finally fixed a variation of this same issue.
This issue was occurring with category archive pagination.
Initially thought it was within the Loop - turns out the theme I was working on had these filters in a file, custom.php, which manually rewrote the permalink structure.
add_filter('category_link', 'themename_change_archive_link', 100, 2);
add_action('init', 'themename_archive_rewrite', 50);
Pagination didn't like that due to what looks like a conflict with a recent WooCommerce update, so I removed these filters, used the Custom Permalink plugin to rewrite the category permalinks, and it worked! Now I have a custom permalink structure AND functioning category pagination.

Categories