WordPress.org

Support

Support » Plugins and Hacks » WP-PageNavi » [Resolved] Pagination page 2 for a certain number of posts breaks

[Resolved] Pagination page 2 for a certain number of posts breaks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Lester Chan

    @gamerz

    ok i couldn’t get that solution working with a post variable so i did it through session + Ajax instead, seems to be working ok for the moment:

    php:

    add_action('wp_ajax_postperpage', 'postperpage');
    add_action('wp_ajax_nopriv_postperpage', 'postperpage');
    
    function postperpage() {
    
        if ( !wp_verify_nonce( $_POST['nonce'], "postperpage_nonce")) {
          exit("unverified request");
       }   
    
    	if(!$_SESSION) {
    			session_start();
    		}
    
    	$postsperpage = sanitize_text_field($_POST['ppage']);
    	$_SESSION['postperpage'] = $postsperpage;
    
    	echo $postsperpage;
        exit();
    }
    
    function my_custom_posts_per_page( $query ) {
    
    		if(!$_SESSION) {
    			session_start();
    		}
    
    		if($_SESSION['postperpage']) {
    			$perpage= $_SESSION['postperpage'];
    		} else {
    			$perpage=20;
    		}
    
            $query->set( 'posts_per_page', $perpage);
    }
    
    add_filter('parse_query', 'my_custom_posts_per_page');

    jquery

    function sortPageSubmit(){
    
    		var postperpage = jQuery('#perpage').val();
    		console.log(postperpage);
    		jQuery.ajax({
    			type: 'POST',
    			url: "/wp-admin/admin-ajax.php",
    			data: {
    				action: 'postperpage',
    				ppage: postperpage,
    				nonce: "<?php echo $nonce ?>",
    			},
    			success: function (output) {
    
    				document.getElementById("sortbox").submit();
    
    			}
    
    		});
    	}

    sample html:

    <select id="perpage" name="perpage" onchange="sortPageSubmit()">
        		<?php echo '<option value="'.$perpage.'" selected="selected">'.$perpage.'</option>';
        		foreach ($postsperpage as $option) {
    				 echo '<option value="'.$option.'">'.$option.'</option>';
    			}?>
    
        	</select>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Pagination page 2 for a certain number of posts breaks’ is closed to new replies.
Skip to toolbar