The parse_query WordPress PHP action fires after the main query variables have been parsed.
Usage
add_action('parse_query', 'your_custom_function_name');
function your_custom_function_name($query) {
// your custom code here
}
Parameters
$query(WP_Query) – The WP_Query instance (passed by reference).
More information
See WordPress Developer Resources: parse_query
Examples
Modify the main query to exclude a specific category
add_action('parse_query', 'exclude_category_from_main_query');
function exclude_category_from_main_query($query) {
if ($query->is_main_query()) {
$query->set('cat', '-5'); // Exclude category with ID 5
}
}
Show only posts from a specific author on the home page
add_action('parse_query', 'filter_homepage_by_author');
function filter_homepage_by_author($query) {
if ($query->is_main_query() && $query->is_home()) {
$query->set('author', '2'); // Show posts from author with ID 2
}
}
Show only posts with a specific tag on the search results page
add_action('parse_query', 'filter_search_results_by_tag');
function filter_search_results_by_tag($query) {
if ($query->is_main_query() && $query->is_search()) {
$query->set('tag', 'featured'); // Show posts with the 'featured' tag
}
}
Limit the number of posts per page on the archive page
add_action('parse_query', 'limit_posts_per_page_on_archive');
function limit_posts_per_page_on_archive($query) {
if ($query->is_main_query() && $query->is_archive()) {
$query->set('posts_per_page', '10'); // Show 10 posts per page
}
}
Order posts by title on the category page
add_action('parse_query', 'order_posts_by_title_on_category');
function order_posts_by_title_on_category($query) {
if ($query->is_main_query() && $query->is_category()) {
$query->set('orderby', 'title');
$query->set('order', 'ASC'); // Order posts by title in ascending order
}
}