Plugin Author
Bowo
(@qriouslad)
@pressthemes1 thank you for the suggestion. How are you currently achieving this? Code snippet? A plugin?… and how valid is this in 2024 with everything google currently is capable of?
its valid in 2024, and im using a code snippet the old AF
Plugin Author
Bowo
(@qriouslad)
“code snippet the old AF”?
@qriouslad this what i’m using since my theme has search filter to filter search result by post format with works but not 100% if you decide to not add my suggestion please let me know what i’m doing wrong with my code instead.
/**
* Change search page slug.
*/
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
$paged = '';
$post_format = '';
if ( get_query_var( 'paged' ) > 0 ) {
$paged = '/page/' . urlencode( get_query_var( 'paged' ) );
}
if ( isset( $_GET['post_format'] ) && ! empty( $_GET['post_format'] ) ) {
$post_format = '/type/' . urlencode( sanitize_text_field( strtolower( $_GET['post_format'] ) ) );
}
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) . $post_format . $paged );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
/**
* Adds the rewrite rule for search with post_format and pagination.
*/
function custom_search_rewrite_rule() {
if ( '' != get_option( 'permalink_structure' ) ) {
// Search with post format
add_rewrite_tag( '%type%', '(.+)', 'post_format=' );
add_rewrite_tag( '%page%', '(.+)', 'paged=' );
add_rewrite_tag( '%search%', '(.+)', 's=' );
// Search with post format and pagination
add_rewrite_rule( 'search/(.+)/type/?([^/]+)/page/?([0-9]{1,})/?$', 'index.php?s=$matches[1]&paged=$matches[2]&post_format=$matches[3]', 'top');
// Search with post format only
add_rewrite_rule( 'search/(.+)/type/?([^/]+)/?$', 'index.php?s=$matches[1]&post_format=$matches[2]', 'top');
// Search with pagination only
add_rewrite_rule( 'search/(.+)/page/?([0-9]{1,})/?$', 'index.php?s=$matches[1]&paged=$matches[2]', 'top');
// Search only
add_rewrite_rule( 'search/(.+)/?$', 'index.php?s=$matches[1]', 'top');
}
}
add_action( 'init', 'custom_search_rewrite_rule' );
/**
* Flush rewrite rules on theme activation or plugin activation (run once).
*/
function flush_search_rewrite_rules() {
custom_search_rewrite_rule();
flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'flush_search_rewrite_rules' );
add_action( 'activated_plugin', 'flush_search_rewrite_rules' );
-
This reply was modified 1 month, 2 weeks ago by DJABHipHop.
Plugin Author
Bowo
(@qriouslad)
@pressthemes1 thank you for the code snippet. This is well noted for now.