WordPress search: pretty URLs (permalinks), custom post types and pagination

Pagination can be done in exactly the same way it works everywhere else in the search archive template.

There’s no need for your custom query and type variable in search.php either. To search for a particular custom post, you can either modify the query, or you can go to that posts archive and append the search query, e.g.

example.com/event_post_type/?s=term

The same is true of any category or taxonomy archive, simply adding ?s=searchterm on the end will turn it into a search query, just like adding /feed/ to the end of URL will give you an RSS2 feed for that particular page/archive content.

You’re task in prettifying this would involve adding an extra rewrite rule to convert that into something nicer, e.g.:

function search_rewrite( $wp_rewrite ) {

    $feed_rules = array(
        'search/(.+)/events'    =>  'index.php?post_type=events&s=". $wp_rewrite->preg_index(1)
    );

    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( "generate_rewrite_rules', 'search_rewrite' );

Would allow you to do example.com/search/searchterm/events, although i advise /events/search/searchterm as a better way of doing it.

Use the monkeyman rewrite rules analyser plugin to test your changes, and you can find more details about rewrite rules in this answer here: Pretty URL with add_query_var

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)