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?
- How do I make my child theme re-apply the settings that were customised when its parent was active?
- How to add a new product type on woocommerce product types? [closed]
- Can WP Customizer work with single pages or posts?
- Enabling Sessions in WordPress 3.0
- Preventing Search Engines Indexing Pages 2, 3 and More?
- How to have a static category/author page?
- How can I specify a category post on my home page
- where can i add custom script to stop header video from autoplay
- How to use get_option() without any filter?
- Is it possible to replace PHP with NodeJS?
- Change separators in HTML tags
- Adding custom field in all widgets, but at the top of the form, in admin area
- How can I use a Tiny MCE editor in my custom Gutenberg block?
- polylang + category/tag custom language link
- How to build a simple nav system of custom post type categories and its posts?
- How to customize and keep things up-to-date?
- What happens to the mu-plugins folder when you upgrade to WordPress 3?
- how to make a custom field readonly or disabled by user role?
- How to make wordpress use a non-wordpress users table?
- Allow multisite admins only to create users with one specific user role?
- Customizing a blog page layout
- Make tag archive display post are ordered by post format
- How to move meta information in TwentyTwelve’s single post template?
- How to Clear Empty Search values?
- Pull Latest Forum Posts within WordPress
- saving the widgets position after drag and drop
- Google calendar in wordpress showing events
- Custom menu not showing even though it shows as being registered?
- Open login logo URL in new tab
- Custom CSS Nav Menu Style Not Being Applied
- Delete a column in edit.php generated with a plugin
- Migrating my site to a new domain does not keep any changes made in customizer
- how to add custom html and css to wordpress
- How to get WordPress Theme Customizer options in the REST API?
- How to use the same custom page template to iframe multiple urls with entry of new url entered from backend
- Creating a WP Test site without /uploads etc
- Where to put coding a validation script for store? [duplicate]
- How to define active widget with js in a customizer
- where can i find the login page in wordpress and add my header to it
- Best way to sort posts with different types by distance
- How come there is no wp-content directory in wp theme
- Image Upload Failing from WP_Editor
- Custom field front end update in wordpress
- What shortcode should I use for
- Database custom table issues – incorrect result displayed
- How to strip thumb from widget
- How to add custom page using them in wordpress 3.5
- Background image no more visible in the preview
- I need to make the title of the page dynamic in my custom plugin
- Custom CSS working on home page but not on NEW created PAGE
- Disable all Fonts that are Standard in WordPress and replacing them with a custom Font?
- Custom Menu (only showing direct cildren) ACF not showing
- Custom product sorting archive page
- Add custom information in editor summary
- A Voting system for a pageants