Wordpress pagination links show same posts as main page why? - wordpress

I am using the following code it works and gives the pagination links, unfortunately each pagination page has same content and main page why?
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array( 'posts_per_page' => 3 ,'paged'=>$paged);
$the_query = new WP_Query( $args );
while ($the_query ->have_posts()) : $the_query -> the_post();
?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <b><?php the_category( ', ' ); ?></b>
</div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
echo $paged;
wp_reset_query(); ?>

Related

Wordpress: authors page with custom query

On a wordpress site I am coding an authors page. Therefore I use a custom query that I also use in several other parts of the site. Partly it works fine, but as it has a major bug, I cannot use it:
I receive not only a chosen autors post. All posts of all authors are listet.
If I use the main loop, I do not have that problem. But because of the pagination which should be the same everywhere, I would like to use the custom loop. Where is my mistake?
Here my code:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>
<section class="tk_fullwidth">
<div id="primary" class="content-area">
<h1 class="page-title">Beiträge von: <?php echo $curauth->nickname; ?></h1>
<div id="main" class="site-main" role="main">
<div class="grid" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 285, "gutter": 20 }'>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '50',
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'date',
) );
?>
<!-- begin loop -->
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="grid-item">
<a class="xyz" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
</div><!-- grid-item -->
<?php endwhile; ?>
<!-- end loop -->
<div class="tk_pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( '<', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( '>', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div><!-- #main -->
</div><!-- #primary -->
</section><!--tk_fullwidth-->

pagination for custom post type archive page

I'm trying to add pagination to following code inside archive-CustomPostType.php page but it doesn't show pagination or it shows empty page.
My code:
<?php get_header(); ?>
<h2 class="ptm pbm mbl text-right red-bg"><span>سریال</span></h2>
<div class="container">
<?php
$terms = get_queried_object();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts(array(
'post_type' => 'series',
'posts_per_page' => 20,
'paged' =>$paged,
'category' => $terms->term_id,
'meta_query' => array(
array(
'key' => 'is_season',
'compare' => '==',
'value' => '0'
)
)
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<!--HTML goes here-->
<?php endforeach; ?>
</ul>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif;
?>
</div>
<?php get_footer(); ?>
I'm using page navi for pagination but I don't think it's the problem because I tried with normal wordpress pagination but didn't help.
I think the issue is coming from get_queried_object and I don't know how to handle this.
Any help would be appreciated.
You Can Try This Code.
<?php get_header(); ?>
<h2 class="ptm pbm mbl text-right red-bg"><span>سریال</span></h2>
<div class="container">
<?php
$terms = get_queried_object();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts(array(
'post_type' => 'series',
'posts_per_page' => 20,
'paged' =>$paged,
'category' => $terms->term_id,
'meta_query' => array(
array(
'key' => 'is_season',
'compare' => '==',
'value' => '0'
)
)
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<!--HTML goes here-->
<?php endforeach; ?>
</ul>
<div class="pagination">
<?php
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => 'Previous',
'next_text' => 'Next',
) );
?>
</div>
<?php else : ?>
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
?>
</div>
<?php get_footer(); ?>
Modifying query post type in functions.php
add_action( 'pre_get_posts' ,'wpse222471_query_post_type_portofolio', 1, 1 );
function wpse222471_query_post_type_portofolio( $query )
{
if ( ! is_admin() && is_post_type_archive( 'customposttype' ) && $query->is_main_query() )
{
$query->set( 'posts_per_page', 6 ); //set query arg ( key, value )
}
}

How to include pagination in a Wordpress Custom Post Type Query

I have the code below:
<?php $the_query = new WP_Query( 'posts_per_page=30&post_type=phcl' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php echo $file; ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i>
<?php echo get_the_title(); ?>
</a>
<div class="file-description">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
I am trying to use paginate_links Wordpress function but no matter where I put it, I can't make it work. Can someone help me with this?
Try the code below:
$the_query = new WP_Query( array('posts_per_page'=>30,
'post_type'=>'phcl',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php the_permalink(); ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
</a>
<div class="file-description"><?php the_content(); ?></div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_postdata();
When querying a loop with new WP_Query set the 'total' parameter to the max_num_pages property of the WP_Query object.
Example of a custom query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'post', // Your post type name
'posts_per_page' => 6,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
// YOUR CODE
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
}
wp_reset_postdata();
?>
Example of paginate_links parameters adapted to the custom query above:
For more reference please visit this link
Try This one, it worked for me!
<?php
$paged = get_query_var('paged');
$args = array(
'post_type' => 'blogs',
'paged' => $paged,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'post_status' => 'publish',
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
?>
Your code...
<?php
}
}
wp_reset_query();
?>
<div class="pagination">
<?php
echo "<div class='fz-pagination'>" . paginate_links(array(
'total' => $the_query->max_num_pages,
'prev_text' => __('<div class="preious-page">Prev</div>'),
'next_text' => __('<div class="next-page">Next</div>')
)) . "</div>";
?>
</div>

How to implement wordpress pagination

I have looked for pagination scripts and put them in the code below but nothing works, how do I implement pagination below?
'<?php
$args = array( 'posts_per_page' => 10 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <b><?php the_category( ', ' ); ?></b>
</div>
</div>
<?php endforeach;
wp_reset_postdata(); ?>'
Try This :
You need to pass paged parameter inside argument.and then pass max_num_pages in function paginate_links
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array( 'posts_per_page' => 10 ,'paged'=>$paged);
$the_query = new WP_Query( $args );
while ($the_query ->have_posts()) : $the_query -> the_post();
?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <b><?php the_category( ', ' ); ?></b>
</div>
</div>
<?php
endwhile;
$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' => $the_query->max_num_pages
) );
wp_reset_query();

Wordpress wp_query custom post type query not working on second page

Using below code i'm listing all posts under custom post type people
<?php $loop = new WP_Query(array('post_type' => 'people', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'wpcf-people-sort-order','posts_per_page' => 4, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
); ?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; ?>
Below is my pagination,
<div class="cus-pagi">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
?>
</div>
Also i created a plugin using above code to display list of titles in sidebar. So whenever i access mysite.com/people both(i.e list of custom posts with pagination & sidebar list of post title) of my custom query are working fine.
If i go to second page, sidebar is showing empty.
did anyone know where i'm going wrong ?
You you need to use the Loop with your custom query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'people',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'wpcf-people-sort-order',
'posts_per_page' => 4,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; endif; wp_reset_postdata(); ?>

Resources