Leave a Reply

2 comments

  1. Instead of implementing a custom search query, instead include the custom fields in the standard search, and use rewrite rules to map /?post_type=cars&s=mysearchterm to /search/cars/mysearchterm . Or just use the query vars in the URL rather than POST/$_POST or transient data. GET/$_GET is your friend here.

    Transients are intended for storing data temporarily rather than recalculating it, e.g. RSS feeds, and other things that are expensive to calculate that may not have a permanent lifetime. They are not a replacement for sessions ( nor should you need sessions or some other analogue ).

    This should show you how to bundle the custom fields in the search:

    http://stv.whtly.com/2010/03/15/extend-wordpress-search-to-include-custom-post-meta/

    Test against $wp->query_vars['s'] or get_query_var( 's' ) to see if your search query is for your post type so you can then conditionally include the custom meta search bits.

    credits: thanks kaiser for the get_query_var tip

  2. Much like @TomJNowell answer.

    With your search, use $_GET instead of $_POST for your search request. Your search variables will carry on from page to page in the URL for your custom pagination.

    Using $_GET is potentially asking for a security breach. Make sure you validate or sanitize your variables. WordPress has a great deal of documentation on Data Validation. Otherwise, read the PHP manual for more methods of Validation or Sanitization.

    As far as your custom pagination goes, I would recommend setting an extra variable in your URL. I’m sure your custom pagination already sets a page number. So you could obtain the page number from your URL, (e.g. $_GET['page']) and adjust your query offset for the search results.

    Hypothetically let’s say you had a URL-friendly pagination, along with the following configuration.

    $number_posts = 10;// Numer of search results to display per page.
    $current_page = $_SERVER['REQUEST_URI'];// Will return the current page values of your URL.
    $sub_page = '/page/';// Current pagination page.
    $using_pagination = strpos($current_page, $sub_page);// Check for paginated content.
    if($using_pagination === false){
        /* Not using the pagination, get $number_posts with no offset. */
    } else{
        $page_number = explode('/page/', $current_page);// Locate just the sub-page from the URL.
        $page_number = preg_replace('/D/', '', $page_number[1]);// Get only the sub-page number from the URL.
        if(is_numeric($page_number)){
            /* Set the correct offset number, based on the number of search results to display. */
            $offset = ($page_number - 1) * $number_posts;
        }
        /* Using the pagination, get $number_posts, with an offset of $offset. */
    }
    

    You could use the above code for your custom pagination to return the proper search results. Note: Remember, you would need to of course pluck out the search variables from the URL, to properly query the correct search results.