• Resolved robertosalemi

    (@robertosalemi)


    Hello to all,
    I customized the work of research, I’ll explain:

    1 ) I made a search input in ajax when use the query :

    $sql = "SELECT DISTINCT(post_title) FROM wp_posts WHERE post_type = 'portfolio' and post_title like '%$q%' and post_status = 'publish' ";

    where $q is the text in the search input.

    2) the final url is: nomesito.it/?s=TermineCercato&post_type=portfolio

    3) in the template file seach.php I added:

    <?php
    if(isset($_GET['post_type']) and $_GET['post_type']=='portfolio'){
    	get_template_part( 'loop' , 'entrySearch');
    } else {
    	get_template_part( 'loop' , 'entry');
    }
    ?>

    so in my case the file is called loop- entrySeach.php where they are displayed only 10 items per page.

    This all works fine.
    The problem occurs when the items are more than 10 and therefore appears pagination.
    The proposed WP url for the second page is nomesito.it/page/2/?s=TermineCercato&post_type=portfolio, to click on it but I reloads the homepage of the site.

    How can I fix?

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Pagination by permalink parameter only works when you work with the original WP_Query object, otherwise you’re on your own, which isn’t that difficult. The page number should be available from the global $wp_query object’s ‘paged’ query var. If not there, it’s somewhere, var_dump the object to find it. Be sure you are working with the main query object with is_main_query().

    Once the page number is determined, add a limit clause (with offset) to the query, something like "LIMIT ($paged-1)*10,10"

    Thread Starter robertosalemi

    (@robertosalemi)

    But I’m using the default functions of WordPress, this is my search.php files:

    		<?php
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		query_posts($query_string .'&posts_per_page=10&paged=' . $paged);
    		if (have_posts()) : ?>
    
    		<header id="page-heading">
    			<!--<h1 id="archive-title"><?php _e('Risultati per', 'adapt'); ?>: <?php the_search_query(); ?></h1>-->
    
    			<?php if ($s!="") { ?>
    			<h1 id="archive-title">
    				Hai cercato: <?php echo urldecode($s); ?> |
    				Risultati: <?php $total_results = $wp_query->found_posts; wp_reset_query(); echo $total_results; ?>
    			</h1>
    			<?php } ?>
    		</header>
    		<!-- /page-heading -->
    
    			<?php
    			if(isset($_GET['post_type']) and $_GET['post_type']=='portfolio'){
    				get_template_part( 'loop' , 'entrySearch');
    			} else {
    				get_template_part( 'loop' , 'entry');
    			}
    			?>
    			<?php pagination(); ?>
    		<!-- /post  -->
    
    		<?php else : ?>
    
            <header id="page-heading">
                <h1 id="archive-title"><?php _e('Risultati della ricerca per', 'adapt'); ?> "<?php the_search_query(); ?>"</h1>
            </header>
            <!-- /page-heading -->
    
            <div id="post" class="post clearfix">
                <?php _e('Nessun risultato trovato.', 'adapt'); ?>
            </div>
    			<!-- /post  -->
    
            <?php endif; ?>
    
    Moderator bcworkz

    (@bcworkz)

    I see, maybe I am confused. I understood you were providing a SQL query string as “input in ajax”. If the results of this query is what is supposed to be paged, then it needs a LIMIT clause as I stated previously, as using this query is replacing the default WP_Query.

    If the paged output is coming from a theme template, one would rightfully expect the template would be using the default WP_Query, but for the code you posted, this is not the case. By calling query_posts() the template is replacing the default WP_Query. It takes the page number to display from the main WP_Query. This usually causes the pagination links to malfunction. In your case the pagination functions seem to work, possibly because the theme uses a custom function for pagination, pagination(), which apparently corrects pagination links.

    The problem here is the use of query_posts(). This is very bad practice for a theme to use this function to drive the main loop of a template. From the Codex page on this function:

    This function isn’t meant to be used by plugins or themes. … query_posts() is overly simplistic and problematic way to modify main query of a page…. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

    You also say that when a page 2 request is sent, that WP “reloads the homepage of the site” and not the search results page. This indicates a deeper error than a simple pagination error. The request parser is not recognizing the search request, so only seeing a non-specific request which results in a home page display. This sort of malfunction could be due to a conflict with one of your plugins.

    Thread Starter robertosalemi

    (@robertosalemi)

    Hi,
    the principal problem was for the combination with two different type of query.

    Now, I use the default query for default search page and custom query for the search page with custom type category.

    Thanks for all!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search Page: pagination does not work’ is closed to new replies.