WordPress.org

Ready to get started?Download WordPress

Forums

Blank search sends you to the homepage (8 posts)

  1. jacobfogg
    Member
    Posted 4 years ago #

    So I have setup my homepage to be a static page instead of the blog. Everything is working great, until one day I realized that a blank blog search sends me to the homepage instead of the search result page.

    Here are the sample urls:

    sampleblog.com/ -- is my static home page
    sampleblog.com/blog/ -- is my blog home
    sampleblog.com/?s=mysearch -- is a populated search, displays the search result page
    sampleblog.com/?s= -- is an empty search, displays the static home page

    So, I am willing to do just about anything to fix this... I would prefer the search page to be sampleblog.com/blog/?s=mysearch ... which would fix the blank search issue.

    I am very PHP savy and have written several plugins at this point, and am willing to do some hacking to make this work, I just need to be pointed in the right direction.

    Thanks!

  2. Shane G.
    Member
    Posted 4 years ago #

    Hi,

    Check with these options;

    -> Change your theme to default in order to rules out theme level issue.
    -> Disable all the plugins
    -. Upgrade your wordpress to the latest version
    -> Add this code in htaccess of your blog:

    php_value memory_limit 64M

    Thanks,

    Shane G.

  3. jacobfogg
    Member
    Posted 4 years ago #

    I went so far as to install a vanilla wp with an empty db and no themes or plugins. I added a page titled "Blog" and left it empty. Then I went to settings->reading and checked "Static page", I chose "About" as the Front page and "blog" as the Posts page. Next I went to the blog page (ttp://localhost/blog/?page_id=3) and performed a search for test(http://localhost/blog/?s=test)... no results showed up but it did display the search result page as expected. Next I did a blank search (http://localhost/blog/?s=) and low and behold the "About" page showed up.

    You can't strip down more than that...

    See no relation to the htaccess change, but Figured I might as well give it a shot... I am testing at this point on a windows box, so I can't create a .htaccess file... I did however tweak the memory limit in my php.ini file... still no dice.

    I understand why this is happening, the search is defaulting to the wordpress root. It is a bug however that '?s=' displays the static home page.

  4. jacobfogg
    Member
    Posted 4 years ago #

    WP does recognize http://localhost/blog/?page_id=3&s=test, so a simple redirect might work... but that means a regular express looking for ?s= or &s=...

    OR

    Along the same lines, modify the code so that search forms look to see if the "blog" is the homepage or an alternate page and adjust the printed URL accordingly

    OR

    Maybe I register a filter to look for something like 'id="searchform" action="http://localhost/blog/"' and replace it with 'id="searchform" action="http://localhost/blog/?page_id=3"'

    OR

    Maybe the fix might be as simple as the code somewhere doing a check like:

    if($_REQUEST['s']!=''){//then this is a search

    If that is the case, a change to something more like

    if(isset($_REQUEST['s'])){//then this is a search

    Might fix the issue as well.

    These are my ideas, I'm willing to try any of them out... just looking for some direction to make sure I am not wasting my time.

  5. jacobfogg
    Member
    Posted 4 years ago #

    AHHH... the function to build the form is:

    get_search_form(), which is found in /wp-includes/general-template.php

    It is setting the action by using get_option('home')

    I would suggest the following logic to replace the simpel get_option('home')

    $searchAction = get_option('home');
    if(get_option('show_on_front') == 'page'){
    $searchAction = get_page_link(get_option('page_for_posts'));
    }

    Then when building the form, set the action to $searchAction.

    For now, I am going to just create a custom function in my funcitons.php file and refer to that to build the search box instead of the default one... but it would be nice to see this fixed!

    Should I report this to bugs?

  6. banesto
    Member
    Posted 4 years ago #

    i had the same problem and after several hours it's resolved:
    wp version 2.9.2.
    in wp-includes/query.php
    line 1244:
    } elseif ( !empty($qv['s']) ) {
    should be changed to:
    } elseif ( isset($_GET['s'])) {

    and line 1724:
    if ( !empty($q['s']) ) {
    should be changed to
    if ( isset($q['s']) ) {

    this way wp will shot all posts if nothing in search bar is entered - exactly the way it's supposed to do :)

    for the record - i try to use search as a filter, so it's not required to enter something specific keywords input, because user can filter posts by categories, users and publish dates.

  7. spitzerg
    Member
    Posted 3 years ago #

    Another option is to add a request filter:

    add_filter( 'request', 'my_request_filter' );
    function my_request_filter( $query_vars ) {
        if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) {
            $query_vars['s'] = " ";
        }
        return $query_vars;
    }

    Then if you're reusing the search query in your search form don't forget to trim it so you don't end up with one or more spaces (just to keep things clean, probably won't affect results.

    <input type="text" name="s" id="s" value="<?php echo trim( get_search_query() ); ?>" />

    Hope this helps, it seems to be working thus far on my site and doesn't involve changing any of the WP code making upgrades easier.

  8. Aga
    Member
    Posted 3 years ago #

    spitzerg, your solusion works great and is simple compered with the ones I found on the net, e.g.:
    1) http://core.trac.wordpress.org/ticket/11330
    2) http://accessites.org/site/2009/01/wordpress-empty-searches/

    Thanks a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.