I haven’t tested this, but I found a sample that was close. I have modified it to be similar to what you want. I’m not sure it is 100%, but it should get you really close:
add_action('pre_get_posts','wpse_filter_parents_only');
function wpse_filter_parents_only($query){
if (!is_admin() && is_category('26')){
$query->set('post_parent',0);
}
return $query;
}
This should update what WP_Query retrieves from the database, and only for category 26. If you need this to affect multiple categories, stuff them into an array and insert the array like this: is_category($cats).
Hope this helps!
Related Posts:
- Filter WordPress Archive Widget to exclude posts with specific custom taxonomy term id
- How can I modify the WordPress default widget output?
- How to hook into unregistering a widget instance?
- Prevent 404 when using pre_get_posts to filter an archive page
- Filter Widget Title Wrap
- Is it possible to remove the filter from 4.8 text widget?
- Anyway to edit the titlebar of WordPress Widgets in the Admin area?
- WordPress tag cloud add more links
- How to add a class to Tag cloud widget link?
- How to make a widget expand wider than the column width when editing its settings in the admin
- Multiple filters for wp_get_archive
- Warning: Missing argument 2 for widget_title filter
- Sorting a specific taxonomy by archive date using URL
- Add Bootstrap Classes to Recent Posts Widget
- include specific Pages to wp_list_pages with filter
- Shortcodes within the sidebar text/html widget: How to preserve the raw HTML output of the shortcode?
- I’ve set up an extra field for all the available widgets. How do I show it’s value in the front-end?
- Widget image reorganize layout
- How can I change the button text of a custom widget?
- WordPress Tag Cloud Filter Prevents Widget HTML from loading
- How can I add a prefix to titles displayed in sidebar using function.php?
- URL to filter posts by post meta similar to shop/?product_cat=”painting”
- Output dynamic_sidebar_params in wp_head
- Hide a specific post from Archive Feed
- Post filter Month dropdown at front-end like wordpress backend
- Filter on widget-title does not work with custom links
- How to use apply_filters(‘get_calendar’) to change get_calendar() output?
- Filter widget_pages_args not working in Pages Widget
- Woocommerce price filter widget always shows max price of most expensive product in the whole shop
- Load More : Admin Ajax 400 Bad request, returning 0
- Changing WooCommerce Display Price Based on User Role & Category [closed]
- Not able to change wp_title using add_filter
- What do add_filters() and apply_filter() do?
- SSL certificate for the host could not be verified
- What to use instead of the content_save_pre filter?
- How to exclude certain widget from showing up on home/front page? [duplicate]
- How can I customize the wp_list_categories
- Unfiltered html in widget title not working
- How can I reliably and globally disable wptexturize?
- Customise Jetpack Publicize text
- Can’t change a label in woocommerce with the normal filter
- WordPress RSS feed – filter RSS content by custom field value
- What is the filter hook for custom fields content?
- Add classname to the_category() function?
- Filter the URL of next_posts_link & previous_posts_link
- Exclude One Category and its Subcategories using WP_LIST_FILTER
- Explanation for remove_filter used in the below code [closed]
- How to change the blog title with add_filter? details below
- Gutenberg disable the “block” tab in right sidebar
- add_filter to ‘woocommerce_before_main_content’ [closed]
- Editing
- the_content filter – checking the post
- add_filter to apply_filters that exists within class
- Disable widgets on specific posts
- Echoing Categories only if they’re a child of a given category
- Adding a title prefix with the the_title filter
- How to removes all instances of thumbnails displayed in my theme?
- How to modify an existing meta description?
- Adding TinyMCE buttons without removing plugin buttons?
- Display Externally Retrieved Data within a Custom Template
- Can the wordpress color palettes by changed through Javascript?
- Filter on the_content doesn’t update the content being searched via register_rest_route
- Where to add_filter in theme functions.php?
- tag removed not using a filter
- How to customize the “Insert/edit link” popup box?
- Wrapping my head around add_filter
- Is it possible to apply filter to meta key value when querying posts?
- How to add_filter to an OOP based apply_filter(‘foo::bar’, $a);
- apply_filters with multiple args and multiple add_filter
- Help with filter for wp_notify_moderator()
- How can I specify the post status of an untrashed post?
- Conflict calling an add_filter() twice
- Filter for author list in gutenberg core editor
- How to append custom text to the output of ‘categories widget’?
- Add filter unless it is being called under specific function
- paginate_links() Change the order of links
- Filter / add_action to upgrade.php page
- Add Filter – Pass Variable (PHP < 5.3)
- Problem width wp_insert_post_data and Gutenberg block editor
- style_loader_tag not changing stylesheet to preload
- Add filter for specific category only
- add_filter doesn’t return false?
- How to track a particular page in order to address the loading speed issue
- Using add_filters() , apply_filter(), add_action() and do_action() in extending a plugin
- Code restricted to dashboard, running unexpectedly in the frontend (pre_get_post and admin-ajax.php)
- Filter for wp_embed_handler_youtube not working
- Post Type Upload Directory – {post_type}_upload_dir filter
- Custom xmlrpc request does not pass parameters?
- Custom registration field to SQL database
- Problem with shortcodes in external file
- WordPress URL rewrite problem
- How to reset/remove added filters ‘posts_join’ and ‘posts_orderby’ after the loop is completed?
- Which hook should be used in this case?
- How to add custom HTML attribute to reusable block div in WordPress admin
- Using two posts_orderby add_filter makes conflict
- Is there a way to check if the ‘wp_sitemaps_enabled’ is true or false?
- Block Gutenberg Editor manually by User Roles with a Filter with code
- How to use add_filter to add the extra data to existing array?
- How to add custom field to top of WordPress Comment Form for both logged in and anon users
- Insert selection from filter everything pro into gravity forms field