WordPress.org

Ready to get started?Download WordPress

Forums

custom query pagination in single.php (1 post)

  1. AliMH
    Member
    Posted 1 year ago #

    i code my custom query and it work in page-test.php template but when i copy/paste it in single-(cpt).php it's pagination stop working and other pages redirect to first page;
    how to fix this issue ?
    here is my code

    <?php
    	global $wpdb;
    	global $wp_query;
    	$temp = $wp_query;
    	$wp_query = null;
    	$wp_query = new WP_Query();
    	$info='comment_ID,comment_post_ID,comment_content,comment_author,user_id';
    	if ( current_user_can('moderate_comments')){
    		$comment_approved = "";
    	}else{
    		$comment_approved = "AND comment_approved ='1'";
    	}
    
    	$query="SELECT $info FROM $wpdb->comments
    			JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
    			JOIN $wpdb->term_relationships ON ($wpdb->term_relationships.object_id = $wpdb->posts.ID)
    			JOIN $wpdb->term_taxonomy ON ($wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id)
    			WHERE
    			comment_type = ''
    			$comment_approved
    			AND post_password = ''
    			AND post_type='recommends'
    			AND term_id = '5135'
    			AND taxonomy='post_types'
    			AND post_status='publish'
    			ORDER BY comment_date_gmt DESC
    			";
    
    	$rows = $wpdb->get_results($query, OBJECT);
    //	$ppp = intval(get_query_var('posts_per_page')); //12 posts per page you might use $ppp = intval(get_query_var('posts_per_page'));
    	$ppp = 2;
    	$wp_query->found_posts = count($rows);
    	$wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);
    	$on_page = intval(get_query_var('paged'));
    	if($on_page == 0){ $on_page = 1; }
    	$offset = ($on_page-1) * $ppp;
    	$wp_query->request = "
    		SELECT $info FROM $wpdb->comments
    			JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
    			JOIN $wpdb->term_relationships ON ($wpdb->term_relationships.object_id = $wpdb->posts.ID)
    			JOIN $wpdb->term_taxonomy ON ($wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id)
    			WHERE
    			comment_type = ''
    			$comment_approved
    			AND post_password = ''
    			AND post_type='recommends'
    			AND term_id = '5135'
    			AND taxonomy='post_types'
    			AND post_status='publish'
    			ORDER BY comment_date_gmt DESC
    			LIMIT $ppp OFFSET $offset";
    	$pageposts = $wpdb->get_results($wp_query->request, OBJECT);
    
    	foreach ($pageposts as $pagepost){
    		print_r ($pagepost);
    	}
    ?>
    <div class="navigation">
    <div class="alignleft prev"><?php previous_posts_link('Previous Page') ?></div>
    <div class="alignright next"><?php next_posts_link('Next Page','') ?></div>
    </div>
    <?php $wp_query = null;  $wp_query = $temp;?>

Topic Closed

This topic has been closed to new replies.

About this Topic