get_posts()
isn’t modifying the main query. The problem is that you’re overwriting the global $post
variable in your foreach
loop. I guess if you’re in a template then you’re in the same scope as the global variable and don’t need to specify global $post;
for this to happen (as you would if you were inside a function). Rename $post
in your loop and the issue will go away:
<?php if (have_posts()):the_post() ?>
<h3><?php the_title() ?></h3>
<?php if (!empty($someOtherPosts = get_posts(['posts_per_page' => 4]))): ?>
<ul>
<?php foreach ($someOtherPosts as $someOtherPost): ?>
<li><?php echo $someOtherPost->post_title ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<h3><?php the_title() ?></h3>
<?php endif; ?>
Related Posts:
- Insert image or ad script after 3 posts using the loop
- Multiple Loops Homepage?
- Why is it necessary to call rewind_posts() when using the loop more than once? [duplicate]
- Is including the loop necessary for page.php? [duplicate]
- Identify the page being shown while in The Loop
- How to find meta_values to call
- Alternate loop output every three posts (within the same original loop)
- Previous and Next Post link doesn’t work
- Sorting Woocommerce products with numeric titles [closed]
- loop not showing up when using a custom page template
- Random post, page inside post loop problem
- Using get_term() in functions.php results in Invalid taxonomy error
- Add Infinite Scroll to Ajax Loaded Category Query
- How to add element after every post in the loop, but not the last one
- single page site with subpages
- Different Queries Return Same Results [closed]
- wordpress simple loop, huge issues
- Check to see if more than one post
- Pagination gives extra page with no results
- Style first 3 posts differently with a container, then the rest of the posts [closed]
- Loop through all posts in a certain year
- What are the disadvantages of using global variable?
- Loop after page content
- the_excerpt not showing posts
- add most viewed post by google analytic in loop [closed]
- Show the page title on blog page
- Query reset problem or flawed code to enable custom post type query
- Sort posts by most recently modified
- How can I make the loop not count special post formats towards the post count?
- How do I order my loop query?
- Orderby my needs in foreach loop like by title or something?
- How to show posts with multiple tags on tag.php?
- Exclude posts from main loop based on meta value
- Using an ACF field from a page within a post loop?
- Blog page Server error 500 with stick post loop in query.php
- How can I paginate this?
- Show all posts using the template page and the loop?
- Why does `inclusive` not work in this wordpress loop?
- How do I automate multiple category loops?
- Change date format
- Custom loop not working
- Loop inside query
- How to stop the continuation of posts on the second loop from where the first ended
- Escape current post from loop
- Why isn’t my multiple loops working?
- How can I display other user avatars on an author profile page?
- something strange is happening to my code
- How to modify search.php to not show pages only show posts
- facebook like, tweet and google+1 buttons inside the loop on home, archive and search pages and missing facebook like buttons
- Loop Offset for tag based “Related Posts”
- Show articles in Pair
- How do I put my frontpage featured image in a page template for home.php?
- Randomly display post based on meta value. (Kind of Priority/Weight)
- Make a loop to return x number of posts, but only if they have content or excerpt
- Closing the loop…featured image variable based on post id
- How do you stop the rendering of the website to have something display on the front-end?
- How to print redux gallery feilds id? [closed]
- add_action in the loop hooks
- How to render the single post template with the post name?
- Reset Loop get_posts();
- How to filter posts by category INSIDE the loop
- static page with multiple loops breaks post pagination
- new custom theme – posts displaying on top of one another
- Offset Loop by 1 , Limit next loop to 1 (Most Recent) Post
- Help with if and else statement
- Building a form with WordPress, and also pulling that data for a user page
- How to show multiple attachements together with Scribu’s Posts 2 Posts connector?
- Database query and get_user_meta return incorrect data
- Need to while loop and display contents in the subfield of a group field given by ACF
- Use the loop to check if DB record exists?
- Customizer settings using “for” loop
- Split Content and Gallery
- Adding comma to while loop wpquery output
- Search – Check if post is a page on search page
- How can I make this iterative conditional within the Loop work via function?
- Repeat code, change variable?
- Foreach loop inside foreach loop?
- Ajax loop and history.js for SEO
- to get a little more content after ‘read more’ inside loop
- Show all child categories associated to post ID within loop
- Ajax category pagination problem
- Unable to login + Redirect loop on mobile
- Recent posts headlines. How to optimize the loop
- Running the Loop outside of WordPress
- how to use custom loop in the_content filter
- Variable not working in sidebar loop
- Twitter bootstrap carousel multiple items in carousel
- 2 loops in blog homepage
- Change loop to display items in order
- wordpress showing all posts instead of date range
- Help with add_filter(‘the_content’, ‘some_function’) and multiple matches
- Loop doesn’t give the thumbnail the right parent (or class)
- How to toggle a div element between two images when click two submit buttons in a wordpress loop?
- Best hook for a function which adds shortcodes to the system?
- Same posts within a paginated page
- Adding vkontakte social “like” button to posts results in multiple buttons on first post only
- wp_mail sending only once inside foreach loop
- How to use orderby on meta_value when using Pods custom database table storage
- wp_mail function not working in user query loop
- How to set and retrieve cookies while in the loop