Override and change the WordPress query in a template page to custom results

WordPress has a lot of flexibility and it is easy to override certain results when and where you need to. It’s the way WordPress organizes and flows objects into this very part of the content you are reading now. How this content gets here is a matter of what the user is asking for, what you program in your website to deliver, and what content is available that matches the criteria you help the user refine to deliver the best results of content or a search list.

Override and tailor results

Below is another way to override the existing query before it goes to any loop to display anything. A loop is nothing more than a way to incrementally cycle through records that meet criteria specified in a query. A query is nothing more than a way to ask for certain records from a database that meets your criteria. Imagine if all your scripts and queries just dumped all the information at once from the database! Your website wouldn’t be very useful, on the contrary, it would be very ineffective.

It is your job as a website owner to make sure your website delivers exactly the content people expect to see. How they expect to see it is based on how you design your website, which isn’t just graphics and a layout — it’s an organized system of data and functions that can logically interact with users. This is another step you can take to make sure your website achieves these goals.

The function below changes the search results of WordPress, it’s just one example how you can tailor pages to deliver what users need using WordPress. The following function will return content from each of these custom post types: faqs, portfolio, and directory_listing. Just change the names of the custom post types in the array to match your custom post types.


add_filter( ‘pre_get_posts’, ‘new_search’ );
/**
* This function modifies the main WordPress query to include post types other than the ‘post’ post type.
*
* @param mixed $query - original query
* @return $query - amended query
*/

function new_search( $query ) {
if ( $query->is_search )
$query->set( 'post_type', array( 'faqs','directory_listing' ) );
return $query;
};

Leave a Reply 0

Your email address will not be published. Required fields are marked *