• Resolved Hans

    (@hmobron)


    Hello there,

    I made my own search form that’s working well, but a can’t the paginate_links working. Every page contains the same results as page 1.
    How can help me in the right direction?

    $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$_REQUEST["start_letter"]."%' AND post_type='product' ");
      //  print_r($mypostids);
    
        $argssearch = array('post__in'=> $mypostids,
                            'post_type'=>'product',
                            'orderby'=>'title',
                            'order'=>'asc',
                            'posts_per_page'=>10,
                            'product_tag'=>urldecode( $_REQUEST["product_tag"]),
                            'product_cat'=>urldecode( $_REQUEST["product_cat"]),
                            'paged=' => $paged);
    
        $querysearch = new WP_Query($argssearch);
        echo "<br/>-".$querysearch->post_count."-".$paged."<br/>" ;
    $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' => $querysearch->max_num_pages,
        'add_args' => array('start_letter'=>$_REQUEST["start_letter"],
                            'product_tag'=>urlencode ($_REQUEST["product_tag"]),
                            'product_cat'=>urlencode ($_REQUEST["product_cat"]),
                            'ZOEK' => 'Zoek'
    ) ));

Viewing 15 replies - 1 through 15 (of 15 total)
  • Put this code at the top of the page
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    and try.

    Thread Starter Hans

    (@hmobron)

    Hello Hakkim,

    Thats a good point, but is is there but outside the code that i copied.
    Thanks anyway.

    Try using query_posts function, where you can pass your search key, post type etc as its arguments directly. That is search key as ‘s’ and post type as just similar to what used earlier. Please check WordPress codex of this function to get more idea. Afrer this function you can simply use WordPress loop and pagination.

    Thread Starter Hans

    (@hmobron)

    How can i search on only one or more start letters on title off a page?

    In a other forum the wrote this way because the like statement exists in wordpress. I read the codex page query_posts and figure it out how to do that.

    Have you used wp_reset_query() function anywhere in this page? if so please remove/comment it because it will clean up the WP query/pagination variables.

    Thread Starter Hans

    (@hmobron)

    Oke i rewrite the hole thing and got now:

    global $wpdb;
         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     // wp_reset_query();
    
    $args = array('s'=> $_REQUEST["start_letter"],
                  'post_type'=>'product',
                  'orderby'=>'title',
                  'order'=>'asc',
                  'product_tag'=>urldecode( $_REQUEST["product_tag"]),
                  'product_cat'=>urldecode( $_REQUEST["product_cat"]),
                  );
    
    query_posts( $args );
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
      'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    	'format' => '?page=%#%',
        'posts_per_page'=>10,
        'paged=' => $paged,
      	'current' => max( 1, get_query_var('paged') ),
    	'total' => $wp_query->max_num_pages,
        'add_args' => array('start_letter'=>$_REQUEST["start_letter"],
                            'product_tag'=>urlencode ($_REQUEST["product_tag"]),
                            'product_cat'=>urlencode ($_REQUEST["product_cat"]),
                            'ZOEK' => 'Zoek'
    ) ));
      echo $wp_query->max_num_pages."-".$paged;

    When is set posts_per_page on 80, i got 80 results. $wp_query->max_num_pages is 0 all the time and there is no pages notation.

    Try wp-paginate plugin instead of default WP page links. Also put WP loop in between query_posts and pagination, because it can set some post variables. I am mot sure whether it will solve the issue but just give it a try.

    Thread Starter Hans

    (@hmobron)

    No did not help.

    You need to add paged and post_per_page in your query (as the query will be executed again when you click next page. Below how I implemented it:

    $pagenum	= isset( $_GET['pagenum'] ) ? intval( $_GET['pagenum'] ) : 1;
    		$ppp		= get_option('frontier_post_ppp') ? get_option('frontier_post_ppp') : 5;
    
    		$args = array(
    				'post_type' 		=> 'post',
    				'post_status' 		=> 'draft, pending, publish',
    				'author'			=>	$current_user->ID,
    				'order'				=> 'DESC',
    				'orderby' 			=> 'post_date',
    				'posts_per_page'    => $ppp,
    				'paged'				=> $pagenum,
    				);
    		$user_posts 	= new WP_Query( $args );

    Then your post loop, and finish with pagination output (ppp is a setting i implemented in my plugin (frontier Post) so admins can set number of post per page, you can hardcode a number)

    $pagination = paginate_links(
    			array(
    				'base' => add_query_arg( 'pagenum', '%#%' ),
    				'format' => '',
    				'prev_text' => __( 'Β«', 'frontier-post' ),
    				'next_text' => __( 'Β»', 'frontier-post' ),
    				'total' => $user_posts->max_num_pages,
    				'current' => $pagenum
    				)
    			);
    
    	if ( $pagination )
    		{
    			echo $pagination;
    		}

    Forgot you need to add the following in the last line of your code:

    //Re-instate $post for the page
    wp_reset_postdata();
    Thread Starter Hans

    (@hmobron)

    Thanks Finj, but no luck. All the pages are still the same. Even the last page, how has only ten products to list (the leftovers)

    Stange,

    When I did it, it didnt work until I got the following in place:

    1.get the previous page number from the page calling the query (again)

    $pagenum	= isset( $_GET['pagenum'] ) ? intval( $_GET['pagenum'] ) : 1;

    And make the page link ($pagination)

    'base' => add_query_arg( 'pagenum', '%#%' ),

    Have you tried to copy my code in and just delete the author part in the query, that way you should be able to check if pagination works. When it does, then change your query

    Thread Starter Hans

    (@hmobron)

    Finj, now a have done it and is working now. :).

    But now something else. As you can see at my first post, i started whit this: $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$_REQUEST["start_letter"]."%' AND post_type='product' ");

    Later on the suggested to use ‘s’=>’s’. Now this way gave results whit all post whit a s in site and that is not what i want.

    I only want post title begin whit the letter s.

    Thread Starter Hans

    (@hmobron)

    Its all working now.

    I reprogrammed $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$_REQUEST["start_letter"]."%' AND post_type='product' "); back in the script an removed ‘s’=> and replaced by post__in.

    When i have time a will search for the divrence between your script and mine.

    Thanks Hakkim and Finnj

    Glad to hear that you solved the issue !! πŸ™‚

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘paginate_links problem’ is closed to new replies.