WordPress pagination not working with excluded posts - php

I'm using an array inside WP_Query, this is for the index.php page. The code is like so
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = new WP_Query( array(
'post__not_in' => $exclude_ids, // This works correctly
'paged' => $paged,
'posts_per_page' => 9, // I added this as a hope but didn't work
) );
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
but the paged is not working. I echod took the $paged out and it is doing 1,2,3 like it should (I'm assuming?) so it that part is working, and the URL is changing and the pagination link recognises it's on page 1,2,3 but the posts are all the same.

Turns out I'm dumb.
I had but the $args as new WP_Query while it should have just been the array.

Related

Wordpress pagination not working/displaying

Currently I am trying to get wordpress pagination to display at the bottom of the page but nothing shows up, if I echo out "paged" it outputs 1 so I believe it is correctly reading the page it is on. The loop I am using is as follows:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'company_list', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 6, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
To echo out the actual pagination at the bottom of the page I am using:
echo paginate_links( $loop );
Currently no pagination is being displayed at the bottom of the page but the loop is working correctly. The page is also not set as a static front page.
Thanks.
1)You do not have to use question mark in your first row
$paged = (get_query_var('paged')) ?
2)also see :
https://stackoverflow.com/a/33757648/10210277

How to assign serial no in WordPress Post summary Page?

I am trying to assign serial no in my WordPress Post Category summary Page. For this I have written following code. It is working fine but problem is in the case of paging.
When I am going to 2nd page or 3rd page it assigns the no from 1 again. Currently I am showing 5 posts/per page. Then in 2nd page it should show post no starting from 6 but it starts from 1 again. Same to 3rd and other pages. How can I show it in 1st page from 1-5 and in 2nd page 6-10 etc?
Below is my code:
<?php
// args
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'post_type' => 'post',
'cat' => 5,
);
$the_query = new WP_Query( $args );
$postNumber = 1;// to give serial no to post
?>
<?php if($the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php echo $postNumber++; ?> <?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php custom_numeric_posts_nav(); ?>
<?php endif; ?>
<?php wp_reset_query();
Taking help from this topic https://wordpress.stackexchange.com/questions/155903/show-number-of-posts-and-number-on-current-page I have been able to solve my problem. Below is my working code:
<?php
// args
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$ppp = get_query_var('posts_per_page');
$end = $ppp * $paged;
$args = array(
'paged' => $paged,
'post_type' => 'post',
'cat' => 5,
);
$the_query = new WP_Query( $args );
$postNumber = $end - $ppp + 1;// to give serial no to post
?>

Wordpress Pagination Only showing 2 pagess

I've set up a basic homepage to show 3 articles per page with pagination to navigate through these pages. At the moment it'll only show pagination for pages 1 & 2 and no more, even though I have 12 articles which result in 4 pages. I'm not quite sure where I'm going wrong here:
<?php
$paged = (get_query_var('paged'))? get_query_var('paged') : '1';
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2
);
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
include(locate_template('content-post.php' ));
endwhile;
?>
<?php the_posts_pagination( array('mid_size' => 3) ); ?>
the_posts_pagination use default WP query so it not work here. Can you please try below code:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
while ($wp_query -> have_posts()) : $wp_query -> the_post();
include(locate_template('content-post.php' ));
endwhile;
the_posts_pagination( array('mid_size' => 3) );
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
Code is tested in work perfect.
Your code looks alright to me. I was having the same issue, I resolved it by matching the posts_per_page with the Blog pages show at most field located in Settings->Reading in the admin dashboard
example:
"posts_per_page" => 6 then Blog pages show at most should be also 6
I hope this helps.
Use this plugin Click here
and use this shortcode for pagination <?php wp_pagenavi(); ?>
and I use exact following loop for my project and its working.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'cat' => '',
'orderby'=> 'date',
'order'=> 'DESC',
'paged' => $paged ,
'posts_per_page' => 3
);
query_posts($args);
if (have_posts()) :
while (have_posts()):
the_post();
endwhile;
endif;
?>
Please try it. Hope it will work for you also.
And If you are displaying your posts on home page, you need to replace
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
with
<?php
if ( get_query_var('paged') )
{
$paged = get_query_var('paged');
}
else if ( get_query_var('page') )
{
$paged = get_query_var('page');
}
else
{
$paged = 1;
}
The problem is that $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; retrieves the data of the main query object but not for the custom one.
I found a solution here https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops

Wordpress pagination is not working when don't preserve the initial query object

On a custom page template on my custom theme I have the below query. When I do it this way the pagination is working :
$myqueryname = $wp_query;
$mypost_args = array( 'post_type' => 'friends', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query($mypost_args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
Some html.....
endwhile;
previous_posts_link('Prev');
next_posts_link('Next');
But when I am using this way the pagination is not working :
$mypost_args = array( 'post_type' => 'friends', 'orderby' => 'title', 'order' => 'ASC');
$loop = new WP_Query( $mypost_args );
while ( $loop->have_posts() ) : $loop->the_post();
Some html.....
endwhile;
previous_posts_link('Prev');
next_posts_link('Next');
wp_reset_query();
I was wondering why is this happening ? What is the difference ? Sorry if my question is vague but this will help me to understand better the way wordpress is working and not just copy-paste codes from forums.
Thanks in advanced.
If you look into the code of the get_next_posts_link (function that next_posts_link calls) it has the following code:
global $paged, $wp_query;
if ( !$max_page )
$max_page = $wp_query->max_num_pages;
...
And this is the usage of next_posts_link
<?php next_posts_link( $label , $max_pages ); ?>
It looks to me that when you use custom query you need to specify $max_pages for pagination to work.
Try modifying your pagination code to this:
previous_posts_link('Prev', $loop->max_num_pages);
next_posts_link('Next', $loop->max_num_pages);

Having trouble displaying posts from only one category in Wordpress

I'm having difficulty pulling only the desired category into an archive page on Wordpress. My code is below, I thought by defining get-category-by-slug this would work, but instead its pulling all of the post from every category into the page.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
query_posts($args);
$x = 0;
while (have_posts()) : the_post();
?>
Any ideas on how to fix this would be appreciated.
I've also tried these combinations with no luck.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
and
<?php $query = new WP_Query( 'category_name=weddings' ); ?>
and
<?php
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
Please do not use query_posts its not the best way to query data its a last resort if you will.
Instead use WP_Query to get posts from one category just do $query = new WP_Query( 'category_name=staff' ); refer to this page for more information on how to get posts from one category using WP_Query.
EDITED
Try this
$the_query = new WP_Query( array(
'post_type' => 'page',
'orderby' => 'date',
'category_name' => 'wedding', //name of category by slug
'order' => 'DESC',
'posts_per_page' => )); // how many posts to show
// Put into the loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
// Restore original Post Data if needed
wp_reset_postdata();

Categories