if you use wp_pagenavi and your pagination not working in wordpress or
Pagination not working with custom query OR
WP-PageNavi not working properly on custom page
SOLUTION 1
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'posts_per_page' => 3,
'paged' => get_query_var('page'),
);
query_posts( $args );
Changed it from paged to page in get_query_var. SOLUTION 2
function get_url_var($name)
{
$strURL = $_SERVER['REQUEST_URI'];
$arrVals = split("/",$strURL);
$found = 0;
foreach ($arrVals as $index => $value)
{
if($value == $name) $found = $index;
}
$place = $found + 1;
return $arrVals[$place];
}
$page = get_url_var('page');
if(is_numeric($page))
{
$countpage=$page;
}else { $countpage = 1; }
// ends here
//next pagination starts
$total_record = count($wpdb->get_results($search_query, ARRAY_A));
$paged = !empty($countpage) ? $countpage: 1;
$post_per_page = get_option('posts_per_page');
$offset = ($paged - 1)*$post_per_page;
$max_num_pages = ceil($total_record/ $post_per_page);
$pagenew = get_query_var('page') ? get_query_var('page') : 1;
SOLUTION 3 with Jquery
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" id="change_page">
<input name="target_page" type="hidden" value="1" id="target_page" />
</form>
<script type="text/javascript">
function go_to_page(target_page){
var page = target_page;
$("#target_page").val(page);
$('#change_page').submit()
}
$(".wp-pagenavi").find("a").each(function(){
var page = $(this).attr("href");
page = page.slice(-3);
page = page.replace("/","");
page = page.replace("/","");
// The if below, only apllies to the first link, since it doesn't use "/page/#" structure.
if(isNaN(page)) { page = 1; }
$(this).attr("href","javascript:void(0)");
$(this).attr("OnClick","go_to_page("+page+")");
console.log(page);
});
</script>
And on top of the page i use this: if(isset($_POST["target_page"])) {
$page = $_POST["target_page"];
} else {
$page = 1;
}
-------------------------------------COMPLETE SOLUTION--------------------------------
advacne search page with custom query
<?php
/*
Template Name: Search Page
*/
?>
<?php get_header(); ?>
<div id="wrap">
<div id="content">
<div class="clearfix"></div>
<?php
// Get data from URL into variables
$_name = $_GET['productname'] != '' ? $_GET['productname'] : '';
$_model = $_GET['sortby'] != '' ? $_GET['sortby'] : '';
$_orderby = $_GET['orderby'] != '' ? $_GET['orderby'] : '';
global $wpdb;
global $post;
global $wp_query;
// Pagination Setup
/*
$posts_per_page = 20;
$start = 0;
$paged = get_query_var( 'paged') ? get_query_var( 'paged', 1 ) : 1; // Current page number
$start = ($paged-1)*$posts_per_page;
*/
if($_model=="price")
{
$search_query = "SELECT SQL_CALC_FOUND_ROWS wplc_posts.ID FROM wplc_posts INNER JOIN wplc_postmeta ON ( wplc_posts.ID = wplc_postmeta.post_id and wplc_postmeta.meta_key='price' ) INNER JOIN wplc_postmeta AS mt1 ON ( wplc_posts.ID = mt1.post_id and wplc_postmeta.meta_key='price' ) WHERE 1=1 AND (wplc_posts.post_title LIKE '%$_name%' OR wplc_posts.post_content LIKE '%$_name%') OR ( wplc_postmeta.meta_key = 'price' AND ( ( ( mt1.meta_key = 'price' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) OR ( mt1.meta_key = 'manufacturer' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) ) ) ) AND wplc_posts.post_type = 'product' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_postmeta.meta_value+0 $_orderby ";
}elseif($_model=="manufacturer")
{
$search_query = "SELECT SQL_CALC_FOUND_ROWS wplc_posts.ID FROM wplc_posts INNER JOIN wplc_postmeta ON ( wplc_posts.ID = wplc_postmeta.post_id and wplc_postmeta.meta_key='manufacturer' ) INNER JOIN wplc_postmeta AS mt1 ON ( wplc_posts.ID = mt1.post_id and wplc_postmeta.meta_key='manufacturer' ) WHERE 1=1 AND (wplc_posts.post_title LIKE '%$_name%' OR wplc_posts.post_content LIKE '%$_name%') OR ( wplc_postmeta.meta_key = 'price' AND ( ( ( mt1.meta_key = 'price' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) OR ( mt1.meta_key = 'manufacturer' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) ) ) ) AND wplc_posts.post_type = 'product' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_postmeta.meta_value $_orderby ";
}elseif($_model=="date")
{
$search_query = "SELECT SQL_CALC_FOUND_ROWS wplc_posts.ID FROM wplc_posts INNER JOIN wplc_postmeta ON ( wplc_posts.ID = wplc_postmeta.post_id and wplc_postmeta.meta_key='manufacturer' ) INNER JOIN wplc_postmeta AS mt1 ON ( wplc_posts.ID = mt1.post_id and wplc_postmeta.meta_key='manufacturer' ) WHERE 1=1 AND (wplc_posts.post_title LIKE '%$_name%' OR wplc_posts.post_content LIKE '%$_name%') OR ( wplc_postmeta.meta_key = 'price' AND ( ( ( mt1.meta_key = 'price' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) OR ( mt1.meta_key = 'manufacturer' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) ) ) ) AND wplc_posts.post_type = 'product' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_posts.post_date $_orderby ";
}else {
$search_query = "SELECT SQL_CALC_FOUND_ROWS wplc_posts.ID FROM wplc_posts INNER JOIN wplc_postmeta ON ( wplc_posts.ID = wplc_postmeta.post_id and wplc_postmeta.meta_key='price' ) INNER JOIN wplc_postmeta AS mt1 ON ( wplc_posts.ID = mt1.post_id and wplc_postmeta.meta_key='price' ) WHERE 1=1 AND (wplc_posts.post_title LIKE '%$_name%' OR wplc_posts.post_content LIKE '%$_name%') OR ( wplc_postmeta.meta_key = 'price' AND ( ( ( mt1.meta_key = 'price' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) OR ( mt1.meta_key = 'manufacturer' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) ) ) ) AND wplc_posts.post_type = 'product' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_posts.post_title $_orderby ";
}
$page = get_url_var('page');
if(is_numeric($page))
{
$countpage=$page;
}else { $countpage = 1; }
// ends here
//next pagination starts
$total_record = count($wpdb->get_results($search_query, ARRAY_A));
$paged = !empty($countpage) ? $countpage: 1;
$post_per_page = get_option('posts_per_page');
$offset = ($paged - 1)*$post_per_page;
$max_num_pages = ceil($total_record/ $post_per_page);
$pagenew = get_query_var('page') ? get_query_var('page') : 1;
//echo "<pre>"; echo "paged = $paged"; echo "post_per_page = $post_per_page"; echo "offset $offset"; echo "max_num = $max_num_pages"; echo "pagenew = $pagenew";
//echo "<pre>"; print_r($page);
//paged = 1post_per_page = 10offset 0max_num = 5
$wp_query->found_posts = $total_record;
// number of pages
$wp_query->max_num_pages = $max_num_pages;
$limit_query = " LIMIT ".$post_per_page." OFFSET ".$offset;
//echo $search_query.$limit_query;
$result = $wpdb->get_results($search_query.$limit_query,OBJECT);// return OBJECT
if($result){
?><div class="contain_all_search_data"> <div class="int_store_container"> <?php
foreach ($result as $rowpost):
//setup_postdata($post);
$row_pro_id = $rowpost->ID;
$row_post_details = get_post($row_pro_id);
$post_pro_title = $row_post_details->post_title;
//echo "<pre>"; print_r($row_post_details);
$meta_values = get_post_meta($row_pro_id, "price", true);
$meta_manufacturer = get_post_meta($row_pro_id, "manufacturer" , true);
$meta_image_large= get_post_meta($row_pro_id, "image_large" , true);
if (strlen($post_pro_title) > 35) {
$product_title = substr($post_pro_title, 0, 35);
$product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
} else {
$product_title = $post_pro_title;
}
//echo "<p>$row_pro_id</p>";
?>
<div class="col-sm-3 product_wrapper">
<div>
<a href="<?php echo $row_post_details->guid; ?>">
<?php if (@getimagesize($meta_image_large) === false) { ?>
<img class="default-img-class" src="<?php bloginfo('template_directory'); ?>/not-available.jpg" alt="<?php echo $product_title; ?>" height="150px" width="150px" />
<?php } else { ?>
<img class="default-img-class" src="<?php echo $meta_image_large; ?>" alt="<?php echo $product_title; ?>" height="150px" width="150px" /><?php } ?>
</a>
</div>
<div class="title_wrapper">
<a class="nodecor" href="<?php echo $row_post_details->guid; ?>" rel="bookmark" title="Permanent Link to <?php echo $product_title; ?> "><?php echo $product_title; ?></a>
<div class="manu_title"><?php if(!empty($meta_manufacturer)) { echo $meta_manufacturer; } ?></div>
</div>
<p class="price_wrapper">$ <?php echo $meta_values; ?></p>
</div>
<?php
endforeach;
?>
</div></div>
<div class="navigation" style="margin-bottom:20px;"><?php wp_pagenavi(); wp_reset_query(); ?></div>
<?php
//next pagination ends here
} else{
?>
<h3>404 Nothing here. Sorry.</h3>
<?php } ?>
<div class="navigation">
<div class="previous panel"><?php //previous_posts_link('« previous ',$max_num_pages)
?>
</div>
<div class="next panel"><?php //next_posts_link('more »',$max_num_pages)
?>
</div>
</div>
</div>
<?php //get_sidebar(); ?>
</div><!--/wrap-->
<?php get_footer(); ?>
Now open your wp_pagenavi core.php and replace all code with this code
<?php
/**
* Template tag: Boxed Style Paging
*
* @param array $args:
* 'before': (string)
* 'after': (string)
* 'options': (string|array) Used to overwrite options set in WP-Admin -> Settings -> PageNavi
*
* @return void|string
*/
global $paged;
function get_url_var($name)
{
$strURL = $_SERVER['REQUEST_URI'];
$arrVals = split("/",$strURL);
$found = 0;
foreach ($arrVals as $index => $value)
{
if($value == $name) $found = $index;
}
$place = $found + 1;
return $arrVals[$place];
}
//ends here
function wp_pagenavi( $args = array() ) {
if ( !is_array( $args ) ) {
$argv = func_get_args();
$args = array();
foreach ( array( 'before', 'after', 'options' ) as $i => $key ) {
$args[ $key ] = isset( $argv[ $i ]) ? $argv[ $i ] : '';
}
}
$page = get_url_var('page');
if(is_numeric($page))
{
$pagedd=$page;
}else { $pagedd = 1; }
$args = wp_parse_args( $args, array(
'before' => '',
'after' => '',
'wrapper_tag' => 'div',
'wrapper_class' => 'wp-pagenavi',
'options' => array(),
'query' => $GLOBALS['wp_query'],
'type' => 'posts',
'echo' => true
) );
extract( $args, EXTR_SKIP );
$options = wp_parse_args( $options, PageNavi_Core::$options->get() );
$instance = new PageNavi_Call( $args );
list( $posts_per_page, $paged, $total_pages ) = $instance->get_pagination_args();
if ( 1 == $total_pages && !$options['always_show'] )
return;
$pages_to_show = absint( $options['num_pages'] );
$larger_page_to_show = absint( $options['num_larger_page_numbers'] );
$larger_page_multiple = absint( $options['larger_page_numbers_multiple'] );
$pages_to_show_minus_1 = $pages_to_show - 1;
$half_page_start = floor( $pages_to_show_minus_1/2 );
$half_page_end = ceil( $pages_to_show_minus_1/2 );
$start_page = $paged - $half_page_start;
if ( $start_page <= 0 )
$start_page = 1;
$end_page = $paged + $half_page_end;
if ( ( $end_page - $start_page ) != $pages_to_show_minus_1 )
$end_page = $start_page + $pages_to_show_minus_1;
if ( $end_page > $total_pages ) {
$start_page = $total_pages - $pages_to_show_minus_1;
$end_page = $total_pages;
}
if ( $start_page < 1 )
$start_page = 1;
// Support for filters to change class names
$class_names = array(
'pages' => apply_filters( 'wp_pagenavi_class_pages', 'pages'),
'first' => apply_filters( 'wp_pagenavi_class_first', 'first' ),
'previouspostslink' => apply_filters( 'wp_pagenavi_class_previouspostslink', 'previouspostslink' ),
'extend' => apply_filters( 'wp_pagenavi_class_extend', 'extend' ),
'smaller' => apply_filters( 'wp_pagenavi_class_smaller', 'smaller' ),
'page' => apply_filters( 'wp_pagenavi_class_page', 'page' ),
'current' => apply_filters( 'wp_pagenavi_class_current', 'current'),
'larger' => apply_filters( 'wp_pagenavi_class_larger', 'larger' ),
'nextpostslink' => apply_filters( 'wp_pagenavi_class_nextpostslink', 'nextpostslink'),
'last' => apply_filters( 'wp_pagenavi_class_last', 'last'),
);
$out = '';
switch ( intval( $options['style'] ) ) {
// Normal
case 1:
// Text
if ( !empty( $options['pages_text'] ) ) {
$pages_text = str_replace(
array( "%CURRENT_PAGE%", "%TOTAL_PAGES%" ),
array( number_format_i18n( $pagedd ), number_format_i18n( $total_pages ) ),
__( $options['pages_text'], 'wp-pagenavi' ) );
$out .= "<span class='{$class_names['pages']}'>$pages_text</span>";
}
if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
// First
$first_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $total_pages ), __( $options['first_text'], 'wp-pagenavi' ) );
$out .= $instance->get_single( 1, $first_text, array(
'class' => $class_names['first']
), '%TOTAL_PAGES%' );
}
// Previous
if ( $paged > 1 && !empty( $options['prev_text'] ) ) {
$out .= $instance->get_single( $paged - 1, $options['prev_text'], array(
'class' => $class_names['previouspostslink'],
'rel' => 'prev'
) );
}
if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
if ( !empty( $options['dotleft_text'] ) )
$out .= "<span class='{$class_names['extend']}'>{$options['dotleft_text']}</span>";
}
// Smaller pages
$larger_pages_array = array();
if ( $larger_page_multiple )
for ( $i = $larger_page_multiple; $i <= $total_pages; $i+= $larger_page_multiple )
$larger_pages_array[] = $i;
$larger_page_start = 0;
foreach ( $larger_pages_array as $larger_page ) {
if ( $larger_page < ($start_page - $half_page_start) && $larger_page_start < $larger_page_to_show ) {
$out .= $instance->get_single( $larger_page, $options['page_text'], array(
'class' => "{$class_names['smaller']} {$class_names['page']}",
) );
$larger_page_start++;
}
}
if ( $larger_page_start )
$out .= "<span class='{$class_names['extend']}'>{$options['dotleft_text']}</span>";
// Page numbers
$timeline = 'smaller';
foreach ( range( $start_page, $end_page ) as $i ) {
if ( $i == $pagedd && !empty( $options['current_text'] ) ) {
$current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] );
$out .= "<span class='{$class_names['current']}'>$current_page_text</span>";
$timeline = 'larger';
} else {
$out .= $instance->get_single( $i, $options['page_text'], array(
'class' => "{$class_names['page']} {$class_names[$timeline]}",
) );
}
}
// Large pages
$larger_page_end = 0;
$larger_page_out = '';
foreach ( $larger_pages_array as $larger_page ) {
if ( $larger_page > ($end_page + $half_page_end) && $larger_page_end < $larger_page_to_show ) {
$larger_page_out .= $instance->get_single( $larger_page, $options['page_text'], array(
'class' => "{$class_names['larger']} {$class_names['page']}",
) );
$larger_page_end++;
}
}
if ( $larger_page_out ) {
$out .= "<span class='{$class_names['extend']}'>{$options['dotright_text']}</span>";
}
$out .= $larger_page_out;
if ( $end_page < $total_pages ) {
if ( !empty( $options['dotright_text'] ) )
$out .= "<span class='{$class_names['extend']}'>{$options['dotright_text']}</span>";
}
// Next
if ( $paged < $total_pages && !empty( $options['next_text'] ) ) {
$out .= $instance->get_single( $paged + 1, $options['next_text'], array(
'class' => $class_names['nextpostslink'],
'rel' => 'next'
) );
}
if ( $end_page < $total_pages ) {
// Last
$out .= $instance->get_single( $total_pages, __( $options['last_text'], 'wp-pagenavi' ), array(
'class' => $class_names['last'],
), '%TOTAL_PAGES%' );
}
break;
// Dropdown
case 2:
$out .= '<form action="" method="get">'."\n";
$out .= '<select size="1" onchange="document.location.href = this.options[this.selectedIndex].value;">'."\n";
foreach ( range( 1, $total_pages ) as $i ) {
$page_num = $i;
if ( $page_num == 1 )
$page_num = 0;
if ( $i == $paged ) {
$current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] );
$out .= '<option value="'.esc_url( $instance->get_url( $page_num ) ).'" selected="selected" class="'.$class_names['current'].'">'.$current_page_text."</option>\n";
} else {
$page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['page_text'] );
$out .= '<option value="'.esc_url( $instance->get_url( $page_num ) ).'">'.$page_text."</option>\n";
}
}
$out .= "</select>\n";
$out .= "</form>\n";
break;
}
$out = $before . "<" . $wrapper_tag . " class='" . $wrapper_class . "'>\n$out\n</" . $wrapper_tag . ">" . $after;
$out = apply_filters( 'wp_pagenavi', $out );
if ( !$echo )
return $out;
echo $out;
}
class PageNavi_Call {
protected $args;
function __construct( $args ) {
$this->args = $args;
}
function __get( $key ) {
return $this->args[ $key ];
}
function get_pagination_args() {
global $numpages;
$query = $this->query;
switch( $this->type ) {
case 'multipart':
// Multipart page
$posts_per_page = 1;
$paged = max( 1, absint( get_query_var( 'page' ) ) );
$total_pages = max( 1, $numpages );
break;
case 'users':
// WP_User_Query
$posts_per_page = $query->query_vars['number'];
$paged = max( 1, floor( $query->query_vars['offset'] / $posts_per_page ) + 1 );
$total_pages = max( 1, ceil( $query->total_users / $posts_per_page ) );
break;
default:
// WP_Query
$posts_per_page = intval( $query->get( 'posts_per_page' ) );
$paged = max( 1, absint( $query->get( 'paged' ) ) );
$total_pages = max( 1, absint( $query->max_num_pages ) );
break;
}
return array( $posts_per_page, $paged, $total_pages );
}
function get_single( $page, $raw_text, $attr, $format = '%PAGE_NUMBER%' ) {
if ( empty( $raw_text ) )
return '';
$text = str_replace( $format, number_format_i18n( $page ), $raw_text );
$attr['href'] = $this->get_url( $page );
return html( 'a', $attr, $text );
}
function get_url( $page ) {
return ( 'multipart' == $this->type ) ? get_multipage_link( $page ) : get_pagenum_link( $page );
}
}
# http://core.trac.wordpress.org/ticket/16973
if ( !function_exists( 'get_multipage_link' ) ) :
function get_multipage_link( $page = 1 ) {
global $post, $wp_rewrite;
if ( 1 == $page ) {
$url = get_permalink();
} else {
if ( '' == get_option('permalink_structure') || in_array( $post->post_status, array( 'draft', 'pending') ) )
$url = add_query_arg( 'page', $page, get_permalink() );
elseif ( 'page' == get_option( 'show_on_front' ) && get_option('page_on_front') == $post->ID )
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $wp_rewrite->pagination_base . "/$page", 'single_paged' );
else
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $page, 'single_paged' );
}
return $url;
}
endif;
// Template tag: Drop Down Menu (Deprecated)
function wp_pagenavi_dropdown() {
wp_pagenavi();
}
class PageNavi_Core {
static $options;
static function init( $options ) {
self::$options = $options;
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'stylesheets' ) );
}
static function stylesheets() {
if ( !self::$options->use_pagenavi_css )
return;
if ( @file_exists( get_stylesheet_directory() . '/pagenavi-css.css' ) )
$css_file = get_stylesheet_directory_uri() . '/pagenavi-css.css';
elseif ( @file_exists( get_template_directory() . '/pagenavi-css.css' ) )
$css_file = get_template_directory_uri() . '/pagenavi-css.css';
else
$css_file = plugins_url( 'pagenavi-css.css', __FILE__ );
wp_enqueue_style( 'wp-pagenavi', $css_file, false, '2.70' );
}
}
------------------------------------------------------------------------------------------------------------
--Other Related Posts--
Add Simple Pagination without using plugin in wordpress
How to create shortcode and use shortcode in Wordpress
Thanks for give us valuable information If you are Looking for WordPress Support , visit on
ReplyDeleteContact WordPress
WordPress Help
WordPress Contact
solution-wp_pagenavi Not Working for Page 2 on custom page WORDPRESS Is Very Helpful. Thanks for such Information
ReplyDeleteLooking for WordPress Support Visit :
WordPress Support
WordPress Technical Support
WordPress Support Phone Number
Superb Information
ReplyDeleteWP Support
Awesome Information
ReplyDelete24/7 WordPress Support
24/7 WP Support
rupatinmi_Boston Chris Yuba click
ReplyDeleteexogdecos
mauventio-be Srinivas Blaschko Movavi Slideshow Maker 8.0.0
ReplyDeleteCoolutils Total CSV Converter 4.1.1.33
Burp Suite Professional 2022.8.2
CyberLink PowerDVD
ticksolsuce