I have a Custom Post Type called "cars" that stores several custom fields along with the standard Title/Description fields. I need users to be able to search through the CPT based on the custom fields matching the user's criteria. I figured that part out and it works, but the problem is pagination. I figured out how to paginate the CPT based on code I found on Google, and it works, but once you leave page 1, you no longer have the search data submitted with the form. I just need to know how I should store this or pass it along.

In regular PHP I would just use a session to store the data, but I've been told not to do that with Wordpress. So, what is the best option? I've been told Transients are the WP version of sessions, but I'm not sure how to efficiently use them for this and how to keep them separate for each user.

I'm not asking for anyone to write this for me -- I just need some help understanding the best concept for this.

2 Answers
2

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 ).

The only thing I'm unsure of here is what to do with multiple search parameters and nice url formatting for multiple pages. For example, if they want make to match Chevrolet and color to match red, how would you suggest doing it with nice URLs? I know I can just use the GET method and leave the variables in the URL, but that's not the nicest looking thing. I'll do it if that's the only solution though.
–
kcarter609Sep 27 '12 at 20:11

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.