wordpress custom pagination - php

I have make a simple wpquery for get all events. Then with an "if" condition I check if the event start today and if yes, show the title post.
My problem is with pagination, because i don't know make a pagination based on the loop result.
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' => 'events',
'posts_per_page' => '5',
'order' => 'ASC',
'paged' => $paged
);
query_posts($args);
if ( have_posts() ) while ( have_posts() ) : the_post();
$event_start = get('event_start');
// if the event start is today show the title post
if($event_start == date('d.m.Y')){
the_title();
}
endwhile;
// PROBLEM: show the pagination for all events
wp_pagenavi();

I would use a WP_Query and use the custom field parameters to only pull the relevant results. This way pagination will work directly and you won't be retrieving and looping over posts that are not required.
As a side note, you may also find this answer useful as to why you shouldn't use query_posts.

Related

Wordpress pagination is not updating content

I am working with a theme that has pagination on the home page for the blog posts. When I click on pg2 (or any page link) it refreshes but shows pg1 content. Is there something missing from the code below that keeps it from working correctly?
<?php
$makenzie_lite_args = array(
'post_type' => 'post',
'paged' => $paged,
'category_name' => 'blogPost'
);
$makenzie_lite_query = new WP_Query( $makenzie_lite_args );
if ( $makenzie_lite_query->have_posts() ) :
// amount of pages
$makenzie_lite_num_pages = $makenzie_lite_query->max_num_pages;
/* Start the Loop */
while ( $makenzie_lite_query->have_posts() ) : $makenzie_lite_query->the_post();
$makenzie_lite_layout = makenzie_lite_get_theme_mod( 'posts_style_template', 'post-s1' );
get_template_part( 'template-parts/listing/' . $makenzie_lite_layout );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
// reset query
wp_reset_postdata(); ?>
EDIT:
I added this code I found on another SE post:
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
and it works, but now the page numbers just expand as I click thru the pages. Go to ameliaislander.com to see what I mean.
Use get_query_var like this:
$makenzie_lite_args = array(
'post_type' => 'post',
'paged' => get_query_var( 'paged' ),
'category_name' => 'blogPost'
);

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

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

Pagination Not Working on custom post type in Wordpress

I have created a Custom Post type and its page template, i want pagination on my custom post type page template
<?php $paged = (get_query_var('page')) ? get_query_var('page') : 1;
$qry = array(
'post_type' => 'property',
'posts_per_page' => '3',
'oerder' => 'ASC',
'page' => $paged,
);
$listing = new WP_Query($qry);
if($listing->have_posts()):
while($listing->have_posts()):
$listing->the_post();
the_title();
endwhile;
endif;
?>
I am using page_navi Plugin for pagination but its not working,
I am also using the wordpress pagination , here is the code
<div class="nav-previous alignleft"><?php get_next_posts_link(); ?></div>
<div class="nav-next alignright"><?php get_previous_posts_link(); ?></div>
but its also not working.
Please Suggest me some Solutions ASAP
Thanks
So that I can just submit this and have you look into this a little easier with some explanation.
It is not page for pagination in Wordpress. It is paged.
So your query should be....
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // changed all page to paged
$qry = array(
'post_type' => 'property',
'posts_per_page' => '3',
'order' => 'ASC', // This was spelled wrong...
'paged' => $paged, // changed page to paged
);
see http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query for full documentation on how to set the paged variable and use it correctly.
Your order was spelled incorrectly and as that is just above paged it could potentially mess something up, but highly doubtful. I would correct it for the expected execution.
Here gallery is custom post type.
function get_all_gallery_posts( $query ) {
if( !is_admin() && $query->is_main_query() && is_post_type_archive( 'gallery' ) ) {
$query->set( 'posts_per_page', '6' );
}
}
add_action( 'pre_get_posts', 'get_all_gallery_posts' );

WordPress: Custom Query

I have a custom query that uses an array merge so I can list articles and custom post type posts in one list, showing 5 at a time. Pagination below allows the user to see the rest of the results. I keep getting pagination errors, but I've tried virtually every variation of the $paged variable in my query to get pagination to work and it doesn't. I know it's me, and probably a simple syntax thing...but I'm stumped. Any ideas? (Note: this page has multiple, other custom queries above the one in question)
Here's my code:
<?php
$loop1 = array_merge( $wp_query->query,
array( 'post_type' => array('post','podcasts', 'cat' => $cat_ID ),
'paged' => ( get_query_var('page') ? get_query_var('page') : 1 ),
'posts_per_page' => 5 ) );
query_posts( $loop1 );
while (have_posts()) : the_post(); ?>
have you tried?
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),

Categories