Displaying post by category from query with pagination - php

Can you tell me what is the problem with my code? my code is to display the custom post by category. my code is perfectly working fine. but when I put a pagination, the code is error now.
<?php
$ourteam_category_check = '4';
$paged = (int) get_query_var('paged'); --> I inserted this
$niche_ourteam_args = array(
'posts_per_page' => 10,
'paged' => $paged, --> I inserted this
'post_type' => 'shop',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
),
'tax_query' => array(
array(
'taxonomy' => 'shop_cat',
'field' => 'term_id',
'terms' => $ourteam_category_check
),
),
);
$niche_ourteam = new WP_Query($niche_ourteam_args);
while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();
?>
--> echo all the items from category 4 here
<?php endwhile; endif; ?>
<div class="page-nav-area">
<?php
if( function_exists('wp_pagenavi') ) {
wp_pagenavi(array('query' => $the_query));
}
?>
</div>

Try this code is working fine for my side
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="the_loop">
<?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;
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'shop',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat->cat_ID, // your categories or you can use 'category__in' => array(2,6)
),
),
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop code goes here.
<?php endwhile; ?>
<?php if ($loop->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $loop;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php wp_reset_postdata(); else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>

Related

Wordpress - Pagination doesn't work with custom loop but working in blog pages

I have a loop with custom post types, and pagination doesn't appear, when I enter the URL with /page/2, /page/3... it shows the content correctly, but links don't appear on the page.
Here is the code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$parent_only_query = new WP_Query(array(
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
));
while ($parent_only_query->have_posts()){
$parent_only_query->the_post();
//content
}
pagination(); ?>
Archive page with pagination working:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php //content ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?>
<?php endif; ?>
<?php pagination(); ?>
add this in your functions.php
function pagination_nav() {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) { ?>
<nav class="pagination" role="navigation">
<div class="nav-previous"><?php next_posts_link( '← Older posts' ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts →' ); ?></div>
</nav>
<?php }
}
display on page.php
<?php pagination_nav(); ?>
pagination can be shown in custom post type archive template and on custom template as well.
Pagination for archive template.
// current page
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array( 'post_type' => 'product',
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
new WP_Query( $args );
// Call pagination function before wp_reset_postdata()
the_posts_pagination( array(
'prev_text' => '<span class="fa fa-angle-left" aria-hidden="true"></span>',
'next_text' => '<span class="fa fa-angle-right" aria-hidden="true"></span>',
'screen_reader_text' => ' ',
'before_page_number' => '',
'mid_size' => 3,
) );
Pagination for custom Template
// Get current page.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array(
post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
$query = new WP_Query( $args );
$totalPage=$query->max_num_pages;
$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' => $totalPage
) );
You can check WordPress official document on Wordpress Codex

list of all post from the current taxonomy of custom post type

I am creating custom post type(cpt) called circular. and the taxonomy for the cpt is circular_category in wordpress
What I want to achieve is generate a list of the titles of all circular custom post type in current taxonomy page.
the permalink /circular_category/free-circular/
I tried with this code with no luck, any ideas?
Here are some variable that can be add to the query
$cir_cat = $wp_query->get_queried_object();
$cat_name = $cir_cat->name;
$cat_id = $cir_cat->term_id;
$cat_slug = $cir_cat->slug ;
The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'circular',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'post_title',
'category__in' => $cat_id,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'circular_category',
)
)
);
$circular_query = new WP_Query( $args );
Now display the list cpt post from current cpt category
<ul id="circulars">
<?php
if($circular_query->have_posts()) :
while($circular_query->have_posts()) : $circular_query->the_post();
?>
<li>
<a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
<?php get_the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php
$total_pages = $circular_query->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' => '/pages/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h3><?php _e('No Circular found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
I have changed the query and its works I'm giving the answer here if someone needed
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'circular',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'post_title',
'paged' => $paged,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'circular_category',
'field' => 'slug',
'terms' => $cat_slug
)
),
);
$circular_query = new WP_Query( $args );

Wordpress WP-PageNavi not working with custom query

I have been trying to solve this for a while now but with no success. I have read other similar posts but they are not working for me.
I have created a custom query with displays the correct results and the pagination shows but when i click on the page 2 etc, the url changes accordingly but the same posts remain.
My custom query is:
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('page'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
and my other code is:
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $sale_properties->max_num_pages > 1 ) : ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<?php wp_pagenavi( array( 'query' => $sale_properties ) ); ?>
<?php }else{ ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', THE_LANG ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">→</span>', THE_LANG ) ); ?></div>
</div><!-- #nav-below -->
<?php }?>
<?php endif; wp_reset_query();?>
I've tried page, paged and:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
Could someone please help me out as it's driving me crazy
Kind regards
S
Replace page with paged in below code:
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('page'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
to
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('paged'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
or put
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
above the query and pass $paged variable in query at the place of
get_query_var('page'),
Hopes it will help.
EDIT:
Wordpress standard way:
place on top:
$big = 999999999;
$current_page = get_query_var( 'paged', 1 );
$args = array(
//your query arguments
'paged' => $current_page
);
$my_query = new WP_Query($args);
Use loop like below:
while ( $my_query->have_posts() ) : $my_query->the_post();
// your code
endwhile;
then
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
) );
place above where you want to print paging.
I had my custom wp query in the functions.php file. After moving it to my custom page template it worked
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('paged'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));

pagination not working for category of custom post type

When I use pagination for custom post type product its working fine but its not working for the categories of custom post type. for ex. pagination working for this http://localhost/wordpress/products/page/2/ and not for this http://localhost/wordpress/products/landscape/page/2/ its always showing the first page content. How to solve this? given below is my code.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'taxonomy' =>'product_cat','term' => $cat_name1,'orderby'=>'post_date','page'=>$paged );
$wp_query = new WP_Query($args);
if($wp_query->have_posts()) : while ($wp_query->have_posts()): $wp_query->the_post();
<div class="product_list">
<?php the_title();?>
</div>
<?php endwhile; ?>
<?php wp_pagenavi( array( 'query' => $wp_query ) );//plugin code ?>
<?php else : ?>
<!-- No posts found -->
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php echo "No Products found for this categoy!." ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
Try this :
-Replace 'page' arguments with 'paged'
-Replace 'taxonomy' with 'tax_query'.
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$taxonomy_terms = get_terms( $taxonomy, array(
'hide_empty' => 0,
'fields' => 'ids'
) );
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $taxonomy_terms,
),
),'orderby'=>'post_date','paged'=>$paged );
In your question, you have used $cat_name1 for terms listing then please use following code:
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $cat_name1,
),
),'orderby'=>'post_date','paged'=>$paged );
Pagination :
please replace wp_pagenavi() function with following code:
$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, $paged ),
'total' => $wp_query->max_num_pages
) );

Paginate for a simple loop

I've got this code which simply displays all the posts for a particular author:
<?php
$all_active_tasks = get_posts(array(
'numberposts' => -1,
'offset' => 0,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post'
)
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
<h2><?php the_title(); ?></h2>
<p><?php echo $category[0]->cat_name; ?></p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
But what I can't figure out is how to paginate the results to say 10 per page. I've looked at the official codex but nothing I tried seemed to work.
Any help is appreciated.
You can use paged together with posts_per_page parameters. WP_Query. You can grab current page like this:
$paged = get_query_var( 'paged' ) ?: ( get_query_var( 'page' ) ?: 1 );
and then use it in your query:
$all_active_tasks = get_posts(array(
'posts_per_page' => 10,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post',
'paged' => $paged
));
In this way if you put /page/2/ at the end of your url, the query will return the posts from 11 to 20.
How to create the pagination itself, you can check these articles:
here and here.
Setting 'numberposts' => -1 means to get all records. You have to set for the first 10 records (0-9 records)
'numberposts' => 10,
'offset' => 0,
And for next 10, (10-19 records)
'numberposts' => 10,
'offset' => 10,
And for next 10, (20-29 records)
'numberposts' => 10,
'offset' => 20,
Try this working code
put this function in functions.php file of your active theme
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('<i class="fa fa-angle-double-left"></i>'),
'next_text' => __('<i class="fa fa-angle-double-right"></i>'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<div class='col-md-12'><nav class='custom-pagination pagination'>";
echo $paginate_links;
echo "</nav></div>";
}
}
and here is your modified code to work for pagination
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$all_active_tasks = get_posts(array(
'posts_per_page' => -1,
'paged' => $paged,
'offset' => 0,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post'
)
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
<h2><?php the_title(); ?></h2>
<p><?php echo $category[0]->cat_name; ?></p>
</div>
<?php endforeach; ?>
<?php if (function_exists(custom_pagination)) {
custom_pagination(count($all_active_tasks),"",$paged);
}?>
<?php wp_reset_postdata(); ?>

Categories