This seems to work, but I would be interested in hearing from others if it’s the “correct” way to provide this sort of override. I’m assuming you’re visiting a category archive page, like http://www.example.com/category/computers/.
// landing page = 188
// category = "computers" (#8)
function custom_wpquery( $query ){
// the main query
global $wp_the_query;
if ( 'computers' === $query->get( 'category_name' ) ) {
if ( $wp_the_query === $query) {
// reset and override the active query
$query->init();
$query->query( 'page_id=188' );
}
}
};
add_filter( 'pre_get_posts', 'custom_wpquery' );
This should probably test $query->is_archive
, as well.
Related Posts:
- How to use filter hook “the_posts” with a function that refers to $this?
- How to filter, restrict and return posts based on custom user meta information
- How to use the query hook/filter?
- Use REGEXP in WP_Query meta_query key
- Is it possible to completely stop WP_Query retrieving posts?
- Whats the difference between post_limits and pre_get_posts?
- How to add taxonomy filter on the query fly?
- Hook/action after WP_Query gets posts to query custom tables for post-related meta
- WP_Query – filter or directly?
- What is the most efficient way of querying posts based on visits and date for current day?
- Changing Posts Per Page and offset with pre_get_posts
- new WP_Query() – what is the earliest valid hook?
- Identify which loop you are hooking into; primary or secondary?
- How to filter sql only for a specific post type
- Ignore a filter on the media library
- How to get any tag ID
- Which filter/action hook gets triggered after a query has been performed?
- Remove ellipsis from the excerpt retrieved using get_the_excerpt()
- Removing posts from the returned wp_query. Cannot use object of type WP_Query as array
- Best practice custom function, where to echo the variables?
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Gravity Forms field entries into wp_query loop [closed]
- Use ‘parse_query’ filter to show posts that from multiple criteria
- How to change posts order on Category and Tag archives?
- is_search was called incorrectly
- What is considered a default query for the request filter?
- Using apply_filters(‘the_content’, $custom_query->post_content) alters output
- Modifying the Search Results
- Query total number of posts
- How to add post_distinct filter to WP_Comment_Query?
- How to choose between hooking into per_get_posts or into parse_query
- Using Ajax to load more posts | Help me change the wording on my button to notify the user
- How to delete taxonomy term when a wordpress user is removed?
- How to create a WordPress Post Filter that users can use to filter posts by year?
- Order by title without taking into account ‘the’
- How can I filter by slug depending on language selection
- Filter posts in category archive page by year using a dropdown
- Set global $wp_query/$post variable for dynamic page generation
- Display filtered results into custom page
- Applying posts_clauses filter to specific queries only
- Add class to first post in custom loop using post_class filter
- WP_Query with different postmeta filter for each categories
- Slow WP_Query with ‘OR’ on meta_query
- Add posts to WP Query object
- Filter Posts from the Main Query
- How can I allow sticky posts but cap the query to 1 post?
- how to access query string in wordpress?
- How can I override one post and make it display content for another post?
- Filter Custom Taxonomy Posts
- Include posts from some categories while excluding from others
- Changing the default wp_search_stopwords
- Filter post query to only show direct children of category
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Utilising an existing page while using the “s” query parameter
- Change AJAX filter from POST to GET for URL Parameters
- Using post_where filter only on main query NOT sidebard new WP_Query
- Filtering posts by archive showing all years
- WP_Query – How to get all posts of specific days of week by custom field date?
- Multiple wp_query on archive page
- Retrieve select tag custom values from array and display them in current page with wp_query?
- How to use a dropdown to filter posts by custom field
- Remove post type filter added by the plugin in the final query
- relation OR instead of AND – Filtered term ID’s in loop
- Can’t order the query result as given posts id’s array
- filter posts by meta key with pagination
- Why does apply_filters behave different inside and outside a loop?
- Custom post type – filter content by custom meta box content
- Elementor posts custom query based on tag [closed]
- Filter sub-category from checkbox form
- Display posts ONLY from the current logged in user and current week/year/month Elementor posts
- My combination of ‘post_type’ and ‘tax_query’ not working?
- Custom meta_query order for Elementor based on post meta key [closed]
- How to add a “base” filter for all posts visible to visitors on the site?
- SEARCH QUERY PLEASE HELP | call_user_func_array() expects parameter 1 to be a valid callback php
- WordPress – Form does not filter the results of taxonomies
- Query Posts | Combining multiple form inputs into query arguments to generate one filter result
- Sorting query_posts() with a complex orderby filter
- WP Query filters active on wrong query
- remove_action – pre_get_posts – does not restore original query
- Custom category page with taxonomy filters
- Insert wp_query after the_content with plugin (filter the_content won’t work)
- Gather same custom field values in one value in a select tag with wp_query
- How to set selected attribute on option after filter query?
- WP_Query doesn’t works inside loop
- Track write actions to the database
- Fetch only categorized posts
- Rewrite rule pagination on different url levels
- Custom query with post_class filter using current_post not working
- Add filter to Orderby Parameter using Array
- Filter WP_Query output before it is accessed (pre_get_posts)?
- $wp_query when new WP_Query
- Loop the posts between two dates selected using jquery datepicker
- query post based on comparison
- Do not show a custom post if it belongs to a certain custom taxonomy
- wp_query->set for several categories
- Adding arguments to WooCommerce Product Loop using AJAX
- Edit WordPress loop, taxonomies, & filter
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- List categories using WP_Query
- I need to get all categories from a WP_Query