In your first code example, you hook into the pre_get_posts
action, which fires during the main query. But instead of manipulating that query, you trigger another one using query_posts()
, which fires the hook again, and you end up with an infinite loop.
With the second code example, you’re just overriding the main query after it’s occurred. But whilst it works, you’ve ended up with a redundant, time-consuming query.
You’re almost there, and props for your efforts, so let’s bring it together:
function wpse_103997_only_posts_by_current_user( $wp_query ) {
if ( is_user_logged_in() && $wp_query->is_main_query() && $wp_query->is_home() )
$wp_query->set( 'author', get_current_user_id() );
}
add_action( 'pre_get_posts', 'wpse_103997_only_posts_by_current_user' );
Related Posts:
- How to exclude latest x posts from a paginated query?
- Query posts distinct authors
- How to get previous 10 days post from a specific date – WP Query
- Hiding posts in a list from specified categories
- How would I get 1 latest post from a query for 5 posts?
- Exclude posts with empty post_content in wp_query
- Improving WP_Query for performance when random posts are ordered
- Reset Popular post query?
- Why WP_Query(‘showposts=5’) shows only 1 post?
- is there a way to show the the post title after the image?
- Blog post per page setting conflicting with custom WP_Query?
- How to exclude latest x posts from a paginated query?
- Random ajax load only works with posts_per_page set to -1
- posts_per_page option limits the number of Gallery items
- wp_posts table: safely remove unused columns to save database storage
- Query Posts by Custom Field
- Nested WP_Query breaking loop
- WordPress query_posts by tag doesn’t work anymore(?)
- Query for first 3 posts to change the look and feel
- How to get posts published on the latest date?
- How to check in functions.php if there is data in a WP_Query?
- Shortcode with ‘year’ parameter
- On what hook can I get the queried object’s final state?
- wp_query with ajax
- How to give classname to post if post has no content?
- WP_Query: Show 10 posts in date order, first three random
- Random posts that always include a particular post?
- WordPress post filter menu
- Query function not executed between element [closed]
- Echo the number of posts being displayed
- WordPress query portfolio posts
- Display 3 posts with different HTML markup using a loop
- Change content off every sixth element
- Show posts from two specific category in WP_Query
- How to create loop of posts except post ID defined via ACF field
- Displaying all posts from other sites on the network on one site
- Multisite how to display merged posts from two sites and sort by latest date?
- Show 5 posts and than 3 posts offset with pagination
- wp_query args with relation
- Manipulate query to show specific post
- wp query custom orderby not custom field
- Sort / Filter Queries
- Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
- Why I have this strange behavior when I try to exlude the featured post from posts visualization?
- How do I control the fallback query after the original query returned cero posts?
- Remove duplicated posts in the loop if post has more than one category
- Adding a category at even positions on main loop with modified pagination
- wp_query select if have comments
- query_posts different amount of posts per page [duplicate]
- Updating post data on save (save_post vs wp_insert_post_data)
- Lost draft under all posts and drafts
- Get user categories with most posts in it
- How to : pagination in 3 different custom loops on the same page
- Display an author’s posts on his own author page
- How safe is renovating $wp_query when doing WP_Query pagination in Page Template
- Exclude the first ‘n’ number of posts of a tag from home page?
- Query posts and return XML
- Multi row post list
- how to change the number of posts returned in a specific loop without making it global?
- Query Posts With Over 1000 Views
- Copy post to separate database with “add_action(….)”
- List authors with the last post title and order by last post date
- How to order posts by meta_value and title
- How to make multiple sections in home pulling posts category wise?
- Hooking into the post editing screen for an existing page only
- wp_query random post
- Adding a blog archive with pagination using WP_Query
- How to get Tags with specific post id
- Order posts alphabetically: how to set order=asc in mysql query?
- How to order posts in an arbitrary order?
- how can i change WP main archives loop to sort by name or title
- Trying to alter the post_content through the_post
- Using wp_list_pages() after calling query_posts()
- how to get post order by post id wp_query?
- Display Posts on Custom Page
- Possible to alternate between two loops
- How to get posts from a current post’s month?
- Wrong post title displayed from loop
- Call Web Services on post first publish
- While loop articles – if statement order
- do more action after I publish a post
- Retrieve only posts from a specific user in wp-admin/edit.php
- Conditionals if tags exist?
- How can I override one post and make it display content for another post?
- How to dynamically parse and transform post/page content before displaying it?
- How to display all posts divided/ordered by post date? [closed]
- Why Query is returning empty array?
- How do I extract just the post ID of the first item in whatever WP_Query returns?
- Show post content and title in diferent divs using WP_Query using a loop
- WordPress website loads more posts than expected
- Why are my paginated posts always returning the same results?
- Ignore latest two posts
- Is it possible to use WP_Query to only pull posts with attachments?
- Get the amount of posts on a current page
- What does WordPress do if I save a post without content/title? [duplicate]
- Adding bootstrap classes to video shortcodes
- Posts – display all posts except a post by an ID
- Show posts by author of membership level (Paid Membership Pro)
- Inner join overrides Advanced Custom Fields plugin’s get_field [closed]
- meta_value timestamp older than now