Created
February 6, 2019 16:15
-
-
Save Langmans/67eb33e4455aedfcd4ac3dff5396ff58 to your computer and use it in GitHub Desktop.
timber: set the search query to the loaded page on the 404 page so you can extend search.twig in 404.twig.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'timber/context/404', function ( array $context ) { | |
global $wp; | |
$url = urldecode( $wp->request ); | |
$context['referer'] = wp_get_referer(); | |
$context['url'] = site_url( $url ); | |
$query = new WP_Query( [ 's' => get_search_query( false ), 'paged' => get_query_var( 'paged' ) ] ); | |
$context['posts'] = new \Timber\PostQuery( $query ); | |
$context['found_posts'] = $query->found_posts; | |
return $context; | |
} ); | |
add_filter( 'get_search_query', function ( $query ) { | |
if ( is_404() && ! get_query_var( 's', false ) === false ) { | |
global $wp; | |
$query = urldecode( $wp->request ); | |
$query = preg_replace( '@/page/[1-9]\d*/?$@', '', $query ); | |
$query = preg_replace( '/\.(html|htm|php|asp|aspx)$/i', '', $query ); | |
$query = preg_replace_callback( '@/([^/]+)(?:/|$)@', function ( $m ) { | |
return ' ' . $m[1] . ' '; | |
}, $query ); | |
var_dump( $query ); | |
} | |
return $query; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment