Home » Hooks » parse_query

The parse_query hook in WordPress is a powerful tool that allows developers to modify the primary WordPress query before it is executed. This hook is used to alter the query parameters of the default loop and can be used to create custom queries based on the needs of a specific website.

When the parse_query hook is called, it passes an instance of the WP_Query object as an argument. This object can be used to modify various aspects of the query, such as the post type, category, tags, author, and more. The parse_query hook is executed early in the WordPress loading process and is often used by developers to modify the query before the content is displayed to the user.

Example Usage:

Let’s say you have a website that displays a custom post type called "Books" and you want to only display books that have a custom field called "featured" set to true. You can use the parse_query hook to modify the query to only display books where the "featured" custom field is set to true.

function custom_book_query( $query ) {
  if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'book' ) ) {
    $query->set( 'meta_key', 'featured' );
    $query->set( 'meta_value', 'true' );
add_action( 'parse_query', 'custom_book_query' );

In this example, we check to see if we are not in the admin area, we are on the main query, and we are on the archive page for the "book" post type. If these conditions are met, we then modify the query to only display books with the "featured" custom field set to true. This is just one example of how the parse_query hook can be used to create custom queries in WordPress.

Learn More on

Register an account to save your snippets or go Pro to get more features.