… It’s a very simple piece of code on the index page of an
otherwise empty theme…
If you are running this code on the index page of your theme and expect it to effect the main query, then you are adding the action too late. The main query runs well before your theme template loads. You will need to put that code in functions.php
in your active theme or in a plugin and use some conditional logic to make sure that it runs only where you want it too. For example…
function just_one( $query ) {
if ( $query->is_main_query() && $query->is_front_page() ) {
$query->set( 'posts_per_page', 1 );
}
}
add_action( 'pre_get_posts', 'just_one' );
I don’t really know what the if
condition should be. Refer to the Codex page already linked to for the conditions available.
Related Posts:
- Sticky Posts exceed posts per page limit
- Theres a way to use $query->set(‘tax_query’ in pre_get_posts filter?
- Modify Taxonomy pages to exclude items in child taxonomies
- Obliterate the main query and replace it
- Post injection – how to exclude the original post
- Adding meta_key via pre_get_posts causes navigation to disappear
- Sorting posts that has a meta value first then the rest of the posts
- pre_get_posts on a page
- How pre_get_posts filter by roles in WP Admin
- Weird problem with pre_get_posts and $query->is_page()
- Date Query to Pull Current and Future Posts
- pre_get_posts all posts and custom post type with certain tag
- Using Sessions to Filter Posts – bad thing?
- pre_get_posts and the blog page
- Archive by custom post type and custom date field
- pre_get_post filter returns results when there should not be
- Trying to exclude first 5 posts from the first page on the homepage
- Exclude a WordPress post from pre_get_posts if a field is null
- Why the ‘date_query’ is not working in ‘pre_get_posts’ hook?
- Only show certain post types in recent posts widget
- pre_get_posts for exclude category
- pre_get_posts: using tax_query only for certain post type
- pre_get_posts dont firing… Anybody knows whats the wrong with my code?
- Restrict Search Query To After Specific Date
- WP_Error not displaying errors
- Show all posts even if URL points to a single one
- How to achieve post_status__not_in?
- Ordering by meta_key
- Using a pre_get_posts filter to search for multiple strings on all meta values
- Removing taxonomy query by pre_get_posts
- Using different parameters for different queries with pre get posts in functions.php
- how to restrict posts_request filter to the main query only
- Automatically applying a pre_get_posts filter for child categories only
- How to override a query and display specific page by ID?
- pre_get_posts variables
- Another query in pre_get_post cause memory issue
- pre_get_posts having no effect on my category archive
- pre_get_posts returns non property object when using posts__not_in
- Override tax_query with pre_get_posts to include other term_ids on a single category
- $query->is_main_query() is causing query’s tax_query to be ignored
- Menu disappears when meta_key changed [closed]
- pre_get_posts with multiple queries
- How to show the last and newest modified post in a custom category?
- Altering the main query using get_post_meta() in pre_get_posts
- Why query by specific date with variables doesn’t return same result that with harcoded integers?
- Sort WordPress Archive by multiple oderby arguments in pre_get_posts action
- Exclude post type with pre_get_posts?
- Exclude page by title for non admins
- problem with setting tax_query in pre_get_posts
- Problem ID to exclude specific posts from category
- Modify Taxonomy pages to exclude items in child taxonomies
- Modify author archive query to combine two queries
- Change post order on archive to be displayed by most commented being ignored by theme
- Complex query using pre_get_posts
- Super confusing ‘pre_get_posts’ behavior with $query->set
- Having trouble with settings terms as array in pre_get_posts
- Override main query for page template
- Querying custom taxonomy on category-specific page is overwritten by function
- how to get content from other site and show it?
- Insert a variable in pre_get_posts
- Extend taxonomy term page with other posts
- Set a custom number of posts on the first page
- Why does this query not SELECT post IDs like a normal query would?
- Please ensure me that I’m not crazy using the pre_get_posts [closed]
- How to pass >= condition filter to my year custom tax_query
- What is the proper way to use pre_get_post?
- pre_get_posts has php notice when not on CPT archive
- Duplicating event posts in wordpress
- When to use WP_query(), query_posts() and pre_get_posts
- How to add taxonomy filter on the query fly?
- RSS feed with specific keyword
- $query->query_var[‘post_type’] not set for pages
- tax_query not working on main query
- pre_get_posts filter using numeric meta_query comparison (from dates)
- pre_get_posts and search query for admin
- How to change posts order on Category and Tag archives?
- pre_get_posts : ‘post__not_in’ doesn’t work with global variable
- orderby meta_value breaks taxonomy term archives
- Exclude posts from main loop based on meta value
- How do I hide posts across all loops based on the value of a custom field?
- pre_get_post alter current post id
- Modify post filter to set custom number of posts per page and exclude child posts
- Modify main query before it run without pre_get_post
- CPT Archive pre_get_posts not working?
- Elementor posts custom query based on tag [closed]
- `offset` WP_Query argument dont work via `pre_get_posts`
- Add terms to a taxonomy archive from within the same taxonomy
- meta_query is overriding default search
- pre_get_posts causings DB error when using ( ‘posts_per_page’, -1)?
- Meta query broken since 4.7.4 Update
- why doesn’t this pre_get_posts code work?
- Custom Post Type shows pagination (w/404) or posts_per_page query, but not both
- Order posts by custom column using pre_get_posts
- Setting a custom $query->query_vars[‘meta_key’] breaks the WordPress menu
- pre_get_posts action doesn’t work
- How to second orderby in “pre_get_posts” by meta value or combine single date and time to timestamp
- WordPress filter function using query modifications
- pre_get_posts causes Custom Posts appear under ‘Pages’ menu
- How to put posts with some taxonomy on top of others in `pre_get_posts`
- Search for meta_query does not return any result if combined with title