It’s important to add guard clauses to prevent modification of other queries. (For example, menus). We are trying to modify the main query here, so we should bail if this is not the main query.
function my_get_posts( $query ) {
// Bail if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Other guard clauses to ensure we don't affect queries unintentionally.
// Sounds like you are just trying to target the homepage, so we could check for that too.
if ( ! is_home() ) {
return;
}
// We only need to modify the query for logged in users.
if ( ! is_user_logged_in() ) {
return;
}
// Modify the query as needed...
}
add_action( 'pre_get_posts', 'my_get_posts' ); // Note that pre_get_posts is an action, not a filter.
Related Posts:
- How to setup a membership system in WordPress?
- How to get current_cat in navbar, in single post
- How can I get posts in a subcategory to display on it’s parent categories archive page?
- How to redirect WordPress home page to custom static HTML page
- Custom category order with get_categories
- setting a specific home page for logged in users
- Custom sidebar category listing?
- On Install, which code sets the ‘home’ option?
- Auto add taxonomy if post has category
- Video to stretch across the entire homepage [closed]
- Show posts on a page based on a category and tag(s)
- Query posts by category AND custom field, then ORDERBY another custom field – help!
- How to add “Read More…” link in twentytwenty
- CSS to remove “comments count” icon from main page [closed]
- how to get the categories for a single product in a hierarchical way
- Automatically check parents (in a taxonomy) when child is selected
- How to change the category url jusy show /category/%category_id% in wordpress?
- Show Featured Post Only On The Homepage
- Make custom page look exactly as homepage
- How can I create a permalink with two categories, with the order based on the parent category?
- Hide text if not homepage
- Why does my Posts page only show a single post when using Thematic?
- How do I list custom taxonomy terms with the links?
- Get whole linked with wp_list_pages
- Adding Multiple “Parents” in get_categories
- Make the home page template my archive/category page template without code duplication
- Get custom text if in Category
- Create Miltiple Category Base
- website is not loading home page (moving from one server to another server)
- Is it possible to edit a wordpress category with Gutenberg?
- How to show related post with category DESC (ASC) IDs of ID current post
- New to WP: links on static homepage are not working
- Hide entry-header for a specific category
- How can I add category variable inside posts?
- Wonderflux Framework Static Homepage
- Custom display for homepage, tag and author pages
- Want home page to click through to two different page types
- Custom Style per Post, How to Display Custom-Styled Single post on Homepage?
- Post thats in Two Categories, only want to display name for one
- woocommerce display product category on checkout page [closed]
- Prevent posts from being published if the ‘Uncategorized’-category or no category is selected
- Show last child Child category instead of first child category on page
- wordpress categories narrow down results
- Modifying posts based on category in TwentyTwentyTwo theme
- Why after customizing Home Page, the site is loading six missing blob sources, slowing down all site?
- Can’t change WordPress / WooCommerce attribute value order
- Unable to edit the 1Homepage
- How to: Easily Move a WordPress Install from Development to Production?
- When to use is_home() vs is_front_page()?
- Is there a flowchart for WordPress loading sequence?
- Essential technical features for high-end WordPress web hosting? [closed]
- How to remove admin menu pages inserted by plugins?
- How to put logs in WordPress
- How to get the Date Format and Time Format settings for use in my template?
- Where are Additional CSS files stored
- Best Practices for Regression Testing WordPress Websites?
- Remove wrapping div and ul from output of wp_nav_menu
- What Is The Use Of map_meta_cap Filter?
- How do you use a CPT as the default home page?
- get post author id outside loop
- Custom Walker: how to get ID in function start_lvl
- Creative uses of WordPress [closed]
- How to *remove* a parent theme page template from a child theme?
- How do I make my child theme re-apply the settings that were customised when its parent was active?
- Multiple Inputs in a Customizer Control
- New WP_Customize API – how does it work under the hood?
- Embedding a SOAP Client into a WordPress Plugin?
- Is there anything that Joomla or Drupal can do that can’t be done in WordPress? [closed]
- Update widget form after drag-and-drop (WP save bug)
- Data sanitization: Best Practices with code examples
- How to benchmark a WordPress installation? [closed]
- Should I delete the default themes?
- Moving WP install from local to live, what about wp_posts GUID?
- Modified wp.media.view.Settings.Gallery in Backbone JS, but editing doesn’t work
- Running Gutenberg React in Development Mode
- Prevent WordPress from automatically installing a new theme each year
- Can we use one WordPress installation for multiple databases, domains and content directories
- Is there a blank theme framework compatible with WP 3.0? [closed]
- Plugin SVN & update API – how are plugins identified?
- Gutenberg editor add a custom category as wrapper for custom blocks
- Users with custom roles not showing in post author select box
- How to use more than 256MB of memory in the admin?
- How do register_sidebar() and get_sidebar() work together?
- How do you get formatted content of a post using the WordPress API?
- Localization: I want the backend: english and frontend in defined language
- Are there any forks of WordPress (and what is different about them)?
- How to Change 404 page title
- Make Custom Metaboxes Collapse by Default
- WordPress Paginate $wpdb->get_results
- Display random categories on the front page (Finding and Editing Theme Functions)
- Custom maintenance page
- Add button to TinyMCE bar without creating a plugin
- Why do I get the timeout warning?
- How to add a new product type on woocommerce product types? [closed]
- How to include own css on wordpress tinymce editor?
- How to disable page delete
- add button to post edit page when post_status=publish
- how to override woocommerce specific loop or archive-product.php [closed]
- Modify existing plugin function using filter (without modifying the plugin directly)
- Rename UPLOADS folder with custom WP_CONTENT_DIR