Parameter Functions
For WordPress to accept the ‘posts_per_page’ parameter, it must be added to the list of accepted query parameters. This must be done outside the search form page or the search results page, or else this parameter is ignored.
1 2 3 4 5 6 7 8 | // Add filter to include search parameters for Search page template add_filter( ‘query_vars’, ‘avp_search_parameter_query_vars’ ); function avp_search_parameter_query_vars( $query_vars ) { $query_vars[] = ‘posts_per_page’; return $query_vars; } |
This section is placed in the ‘functions.php’ file so that it is called whenever a search query is made.
For WordPress to properly parse the category and tag parameters, a second function must be called to convert the query arguments from what is translated by WordPress into strings recognized by the query operation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | add_filter( ‘request’, ‘avp_search_parameter_set_vars’ ); function avp_search_parameter_set_vars( $vars ) { if ( empty( $vars[ ‘s’ ] ) ) return $vars; // Categories use the category ID for the search if ( isset( $vars[ ‘cat’ ] ) && ( is_array( $vars[ ‘cat’ ] ) ) && ( is_array( $_GET[ ‘cat’ ] ) ) ) { $vars[ ‘cat’ ] = implode( ‘,’, array_map( ‘intval’, $_GET[ ‘cat’ ] ) ); } // Tags use tag slugs for the search if ( isset( $vars[ ‘tag’ ] ) && ( is_array( $vars[ ‘tag’ ] ) ) && ( is_array( $_GET[ ‘tag’ ] ) ) ) { $vars[ ‘tag’ ] = implode( ‘,’, $_GET[ ‘tag’ ] ); } $vars[ ‘post_type’ ] = $_GET[ ‘post_type’ ]; return $vars; } |
This function is placed in the ‘functions.php’ file.
The short URL of the present article is: http://www.terryobrien.me/cnu2p
Personal Links