• I am currently learning and hacking away at twenty eleven, I was trying to figure out how to customise the 404 page to take the url data as a search value, so, if the user typed site.com/2011/12/random-blog-posv/ (see the typo at the end)

    The twenty eleven 404 page comes with a search bar, I want to take ‘2011’, ’12’ and ‘random blog posv’ as arguments and show the search result
    (by this it would be possible to type a blog post and get it as the first result on the 404 page).

    What I need to know:
    – How to get the url string (I assume I’ll have to break it apart myself or is there a function for that..?
    – how to display the results of that search on the 404 page (I assume I’ll take most from search.php but replace $s with $url or some other var)

    Thanks in advance for any advice.

    Mark

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter totoro3

    (@totoro3)

    So far I have this in the head:

    /* PHP code from the web */
    $userSearch = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    
    /* This line only needed on localhost because mamp adds :8888 */
    $url_base = str_replace ( ':8888' , '' , get_bloginfo('url'));
    
    $replace = ' ';
    $xyz = str_replace ($url_base , $replace , $userSearch , $count );
    $otherStuff = array("/", "-", "=", "?");
    $query = str_replace ($otherStuff , $replace , $xyz , $count );
    
    // The Query
    $the_query = new WP_Query( $query );

    Then I added this to the body:

    <ul>
    <?php
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<li>';
    	echo '<a>'.the_title().'</a>';
    	echo '</li>';
    endwhile;
    // Reset Post Data
    wp_reset_postdata();
    ?>
    
    </ul>

    I know the links dont work. It just returns a list of all posts (not a list of posts with the query text in the title or body for example), I am guessing the way I made the query is wrong (WP_Query( $query ) where $query is “some text” from: mysite.com/some-text/). Any advice would be appreciated.

    Mark

    Thread Starter totoro3

    (@totoro3)

    Can anyone help me with this? I have the argument (what was typed into the url), I need to know how to display the results of a search for this information in the 404 page.

    eg:

    1. Type site.com/name-of-post into the url
    2. Get “name” “of” and “Post” as arguments in a search
    3. Display a 404 page with the search results displayed

    I am not sure how to get the search to work with the above code. any advice would be appreciated.

    Mark

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘getting the user imput for 404 search – beginner’ is closed to new replies.