Wordpress WP-PageNavi not working with custom query - php

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'),),),));

Related

Wordpress Pagination not work in category page

I want to display pagination on category pages of WordPress theme
This is my code
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$posts = get_posts(array(
'numberposts' => 150,
'posts_per_page'=>16,
'offset' => 0,
'category__not_inint' => array($category),
'post_status'=>'publish',
'order'=>'ASC'
));
foreach($posts as $post){
setup_postdata($post);
$city_name = get_field( "city-name" );
$display = '' . $city_name . '';
}
the_posts_pagination();
wp_reset_query();
return $display;
and displays the pagination. But the results of all pages are similar to the first page.
You shouldn't be using get_posts if you need the query to be paginated.
Whilst it can be done, this is a total ball ache to achieve. Instead, you should be looking at WP_Query.
Further reading on WP_Query - WP_Query # wordpress.org
Your code could look something like the following;
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1; // explain to wordpress we need this paged
$wp_query = new WP_Query(array( // the query
'post_type' => 'post',
'post_category' => '',
'post_status' => 'publish',
'numberposts' => 150,
'posts_per_page' => 15,
//'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged));
while ($wp_query->have_posts()) : $wp_query->the_post(); // the loop
// some code to make it look pretty
?>
<div class="post-grid">
<a href="<?php the_permalink(); ?>">
<h3 class="card-title"><?php the_title(); ?> </h3>
</a>
</div>
<?php endwhile;
echo ( paginate_links($args = array(
'base' => site_url().'%_%', // site_url prefix is needed for pagination on homepage
'format' => '?page=%#%',
'total' => $wp_query->max_num_pages,
'current' => $paged,
'show_all' => false,
'end_size' => 2,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => 'Prev',
'next_text' => 'Next',
'type' => 'list',
'add_args' => false,
'add_fragment' => ''
)));
wp_reset_query();

Displaying post by category from query with pagination

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; ?>

Can't paginate a custom post type query

Trying to show custom post type offers one by one, paginating them in a custom page template by 1. But it's still showing all the posts.
Here is the code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';
$args = array(
'post_type' => 'offers',
'paged' => $paged,
'post_per_page' => 1,
'orderby' => 'meta_value_num',
'meta_key' => 'offer_order',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'campaigns',
'field' => 'slug',
'terms' => array(
'test-campaign'
)
)
)
);
$new = new WP_Query( $args );
if ( have_posts() ) while ($new->have_posts()) : $new->the_post(); ?>
<div>Post Layout</div>
<?php
endwhile;
wp_reset_postdata();
get_footer();
?>
Replace post_per_page with posts_per_pages in your Array args
——
After endwhile and before of wp_reset_postdata insert this code
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $new->max_num_pages
) );

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 Media Count and Next Page Navigation

Users are allowed to upload and access media files from front end. I am using following code which retrieve media files and show on user front end. I have 40 media files in my wordpress uploads but I want to show e.g. 20 media files at single page and I wonder if someone can help to write previous & next page navigation code for media files. e.g Page 1 2 3 ... 10
<?php
$args = array(
'post_type' => 'attachment',
/* 'posts_per_page' => '2', */
'numberposts' => -1,
'post_status' => null,
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
echo '<tr><td><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
echo ($attachment->_wp_attached_file);
echo '</a>
</td>
</tr>';
}
?>
You can try to use the standard wordpress navigation.
Try to change your $args like this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 20,
'paged' => $paged
);
UPDATE:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 20,
'paged' => $paged,
'numberposts' => -1,
'post_status' => null,
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
echo '<tr><td><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
echo ($attachment->_wp_attached_file);
echo '</a>
</td>
</tr>';
}
?>
UPDATE 2
Also you need to use this function
Post it somewhere below your posts
$big = 999999999; // need an unlikely integer
$navArgs = array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?page=%#%',
'total' => 1,
'current' => max( 1, get_query_var('paged') ),
'show_all' => False,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => True,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain',
'add_args' => False,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
);
echo paginate_links( $navArgs );
As you can see you can set navArgs the way you like.
You need to get a total count of your posts and set the 'total' parameter.
Finally I wrote my solution
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'attachment',
'posts_per_page'=> '10',
'paged' => $page,
'numberposts'=> -1,
'post_status' => 'any',
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
'number' => $display_count,
);
// Custom query, display posts by args defined users
$file_query = new WP_Query( $args );
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
echo '<tr><td><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
echo ($attachment->_wp_attached_file);
echo '</a>
</td>
</tr>';
}
}
?>
Call query funtion in in your code
<?php previous_posts_link( '<< Previous Page', $file_query->max_num_pages ); ?> | <?php next_posts_link( 'Next Page >>', $file_query->max_num_pages ); ?>

Categories