WordPress.org

Ready to get started?Download WordPress

Forums

Have more search results show up (25 posts)

  1. jwild
    Member
    Posted 4 years ago #

    Hello,
    the question I have is, is it possible to make more search results show up when someone searches for a keyword on my wordpress only 3 results will show up. I am guessing only 3 will show up because I have my wordpress set on the main page so that only 3 posts show at one time.

    Is there any way to set the search results higher so more results will show up without changing the amount of posts that shows up on my main page?

  2. henkholland
    Member
    Posted 4 years ago #

    Hi Jwild,
    Good point. I never noticed that because I have 10 posts on my main page. A bit of searching gave me this and it works:
    http://wordpress.org/support/topic/287182?replies=6

    especially this:
    Assuming your theme has got a search.php, open it up, find this..
    <?php if (have_posts()) : ?>
    Above that, place this code..
    <?php query_posts('showposts=999999'); ?>

    Of course if you want the search the results less then nearly a million then change the number... ;)

    Tested as working. CREDIT t31os_

    EDIT: it does NOT work for me on WP 2.7.1. The search results produces the set number of newest posts only. I will search further. To be more precise: if the search word does not exist, I get "nothing found" but if the searchword exists, I get the set number of newest posts.

  3. Though it's a bit outdated, Custom Query String Reloaded lets you set different numbers of posts displayed for searches, categories, etc. I use it and it works fine under 2.8.4.

  4. henkholland
    Member
    Posted 4 years ago #

    Thanks songdogtech. For the moment I will stick to my 10 results since also my previous-next-posts-links work fine on the search result page. Don't want too much scrolling.
    Maybe jwild can use it. I also asked t31os_ in the other topic why it did not work for me.

  5. jwild
    Member
    Posted 4 years ago #

    henkholland,
    I just tried that and when I searched it will bring up every result/post even if the keyword that I searched for was not in that post. lol.

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Maybe try showposts=-1 ... (minus one should be "all")..

    You should be able to over-ride the total amount of posts returned for any page, providing you set the parameters for the current query (which is what you're essentially doing by placing a query_posts call in).

    Unless you have other queries happening before this loop occurs there's no reason you shouldn't be able to grab, 1, 10, 100 or 100000, or however many posts you want.

  7. jwild
    Member
    Posted 4 years ago #

    just tried the -1 , still does the same thing, it just shows a list of recent posts that do not even have the keyword which was searched for :O(

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    Guess i'm off to test then...

    I'll assume anyone trying this is doing so in the theme's search.php and i'll try to replicate.

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    OMG that was fast, works for me...

    Can the people with problems please describe how they are creating their search pages.

    My theme didn't have one, so i copied the one from the default theme and added the query_posts line in..query_posts('showposts=999'); just while testing..

    There's no way i'd have that many results, but it definately works..

    Filename: search.php

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header(); ?>
    
    	<div id="content" class="narrowcolumn" role="main">
    
    	<?php query_posts('showposts=999'); if (have_posts()) : ?>
    
    		<h2 class="pagetitle">Search Results</h2>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    		<br />
    		<br />
    
    		<?php while (have_posts()) : the_post(); ?>
    
    			<div <?php post_class() ?>>
    				<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    				<small><?php the_time('l, F jS, Y') ?></small>
    
    				<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    			</div>
    
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">No posts found. Try a different search?</h2>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Taken straight from the default theme, and all i've done is add the query_posts bit...

  10. jwild
    Member
    Posted 4 years ago #

    I am using the http://iamww.com/wordpress-theme-iamww-w2/ theme, it has a search already on it.. I am also using WordPress 2.8.4.

    I even just tried cutting and pasting the default search php that you modded, and it still did the same thing where it shows all of the new posts instead of search results.

    So I am guessing maybe its something to do with the theme

  11. Custom Query String Reloaded also handles adding the "Next" link on a search page if you have more results than the query string is set for. The plugin hooks into the Loop and intercepts the search results (or any other category, tag, etc. results) the string is set for, without needing to edit a search.php file. Unless the search.php file as something other than the standard loop:

    <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
    
    HTML for title, date, etc....
    
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <?php endif; ?>
  12. jwild
    Member
    Posted 4 years ago #

    So there is no way to do this without an additional plugin I am guessing?

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    I've covered how to do it already.

    Try another theme if you think that may be the problem. Not suggesting that you permenantly change theme, just simply to see if the problem resolves itself by switching to another.. if not then i can only guess a plugin is causing a problem.

    You most certainly do not need a plugin to handle search results, though may want to use one if you want to broaden the scope of search functionality.

  14. henkholland
    Member
    Posted 4 years ago #

    t31os_, thanks for replying so fast. Luckily I have a testing copy of my 2.7.1 site at http://www.enduro.nl/wptest This is what I got:
    1. This one:

    <div id="container">
    <p> Dit is search.php</p>
    <?php query_posts('showposts=999'); if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

    NEGLECTS THE SEARCH WORD AND GIVES MANY RESULTS

    2. This one:

    <div id="container">
    <p> Dit is search.php</p>
    <?php query_posts('showposts=999'); ?>
    <?php if (have_posts()) : ?>

    IDEM, NEGLECTS THE SEARCH WORD AND GIVES MANY RESULTS

    3. This one:

    <div id="container">
    <p> Dit is search.php</p>
    <?php if (have_posts()) : ?>
    <?php query_posts('showposts=999'); ?>

    GIVES A NO RESULT WHEN THE SEARCH WORD IS NOT ON MY SITE AND GIVES MANY RESULTS, NON RELATED POSTS TOO FOR A SPECIFIC SEACHWORD.

    Mind you, lowering the number to 3 works. It then gives three newest posts, unrelated to searchword.

    4. This one: Adding

    <?php query_posts('showposts=3'); if (have_posts()) : ?>
    to the search.php from the default theme acts like nr 3.

    I'll leave my testsite in status nr 4.

  15. This issue has gotten muddy: what exactly do you want to get with your search results? A different number of results than default?

  16. Mark / t31os
    Moderator
    Posted 4 years ago #

    Certainly look like search results here henk.
    enduro.nl/wptest/index.php?s=wassink

    I'm looking at your example pieces of code above and half wondering if you're including all the loop code in each example...

    Item 3: The query_posts is placed incorrectly after the if() , that IF statement refers to the query, so should come after query_posts ..not before..

    2.7.1? ... if it's a test install, plonk the current version on there.

  17. henkholland
    Member
    Posted 4 years ago #

    Yes sir, and t31os_ tip in another thread did not work for the topic starter and not for me, so then he jumped in and we are testing now.

  18. henkholland
    Member
    Posted 4 years ago #

    Hi t31os_,
    Your example enduro.nl/wptest/index.php?s=wassink gives three results where the third found post does not contain the word "wassink". They are just the three newest posts. Try "markelo" gives the same three and is not in any of them.

    2.7.1? ... if it's a test install, plonk the current version on there.

    Yes I will later but not in the next two months or so. I am in the busy season now, and have to travel abroad. No the good moment for a change; I need it to be equal now for other little tryouts. Could be a 2.7.1 related problem.
    I don't know what the topic starter is running.
    I have 10 frontview posts and so I have 10 search results in my normal setting, so I am happy enough.

  19. Mark / t31os
    Moderator
    Posted 4 years ago #

    I've been working on my site today, and i've come to realise something.

    You were indeed correct regarding search results when setting parameters inside query_posts.

    The moment you start plonking any paramters into the query, you're over-writing the query string that has been passed into it by the search..

    So as it stands (unless i'm mistaken), you cannot adjust the search results shown, you can however view more search results by using Prev/Next links as you would with regular entries.

    So simply put, whatever you have set as "posts to show" (or whatever it's called in the admin) will be how many results you get on the search results (per page)...

    Any adjustment to the query_posts line, will effectively re-write the query (losing the search paramters)...

    NOTES: You can see the current search query in get_query_vars('s'), and you'll soon see any adjustment to query_posts causes the data stored there to be lost..

    There's also other info you can look at in get_query_vars, such as posts per page, and how many results were found in the search, etc.. (and various other bits).

    Hope that clears things up a little, and i admit, i was indeed incorrect and a little fast to jump the gun before.. ;)

  20. What about a new query? I use multiple new queries in pages and posts that work without overwriting the main loop:

    <?php $my_query = new WP_Query(''); while ($my_query->have_posts()): $my_query->the_post(); ?><?php endwhile; endif; ?>

    Some possibly helpful examples with search count parameters, but not search results.

  21. Mark / t31os
    Moderator
    Posted 4 years ago #

    Ah hah!... i didn't realise you can pass search parameters into the query, i'm going to give that a test now...

    Thanks for the link above..

    Will report back shortly..

  22. Mark / t31os
    Moderator
    Posted 4 years ago #

    I suppose it makes perfect sense that the search query would work inside query_posts.. with a little reflection on what should have been obvious to begin with (lol), i've written an update to the code i posted before..

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header(); ?>
    
    	<div id="content" class="narrowcolumn" role="main">
    
    	<?php
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$posts_per_page = 10;
    	$args = array(
    		'posts_per_page' => $posts_per_page,
    		's' => $s,
    		'paged' => $paged
    	);
    	query_posts( $args );
    
    	if (have_posts()) :
    	$page_count = $paged * $posts_per_page;
    	?>
    		<h2 class="pagetitle">Search Results</h2>
    		<h4>Showing
    		<?php
    		switch($paged) {
    			case 1 :
    				echo $page_count - $posts_per_page . ' - ';
    			break;
    			case $paged > 1 :
    				echo $page_count - $posts_per_page + 1 . ' - ';
    			break;
    		}
    		if($wp_query->found_posts < $page_count) { echo $wp_query->found_posts; }
    		else { echo $page_count; }
    		?> of
    		<?php echo $wp_query->found_posts; ?> results</h4>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    		<br />
    		<br />
    
    		<?php while (have_posts()) : the_post(); ?>
    
    			<div <?php post_class() ?>>
    				<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    				<small><?php the_time('l, F jS, Y') ?></small>
    
    				<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    			</div>
    
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">No posts found. Try a different search?</h2>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Displays the result count, works with paging, and adjusts the "Showing results" numbers to match the page..

    Don't use showposts in the query though, because it's up to the search to determine "how many" posts (total) will return in the result.. all you need do is adjust the "posts per page" parameter to suit how many you want on each given page.

  23. Cool. That works. Might have to grab that and integrate it into my search page and then not use the plugins Custom Query String and Results Count, as I only use CQS for search anyway.... Thanks

  24. Mark / t31os
    Moderator
    Posted 4 years ago #

    Here's the current version i've been playing with, i've included the code i've used to generate the search form inside comments toward the bottom.

    http://wordpress.pastebin.com/f24b1ee9e

    Few quirks, and a little inline CSS (i'm still playing with it), but it might be of use to someone..

  25. randyhoyt
    Member
    Posted 4 years ago #

    I think this is what you had in mind:
    lohmantrading.com/Fourced/2009/08/modifying-the-posts-per-page-query-in-wordpress/

    It allows you change only a single parameter for the query, leaving everything else untouched. I am using this on a blog where the main pages only show 6 posts but the search results page shows 20 posts.

Topic Closed

This topic has been closed to new replies.

About this Topic