You can instantly access any position in an array by specifying the index.
i.e. to get the first element of the $search_results array:
$first_result = $search_results[0];
Note if you’re not used to working with arrays, they are “0” indexed in most languages, which means the first element’s index is 0, the second is 1, etc.
This is what your loop is basically doing as well. Each time it accesses a different number directly, $search_results[$i] is just putting the value of $i in the same spot as I had the 0 above.
You can also change the value of $i in your loop if you want to start the loop over, for instance. Adding the line:
$i = 0;
…would send you back to the beginning. However I would not recommend you work with the index of a loop until you are quite comfortable with them.
Related Posts:
- Inject post (from specific category) between posts in Loop
- User Defined order on get_categories?
- Custom Loop through category menu to include sub categories
- How to exclude specific category from the get_the_category(); array
- Filter Select results based on selection
- Alert Bar section within WP loop is displaying even though there are no posts
- Display category name only once inside loop
- I’m trying to create an array with a foreach loop, but the array only stores the last item [closed]
- How do I remove a category from a wordpress loop>
- How to get the last category name of a child category?
- How to get current post category details inside “loop”?
- Problem with custom loop and wp_list_pluck [closed]
- Echoing a CSS class based on category of post in a list
- Do not duplicate posts with multiple categories in multiple loops
- Exclude category and post from loop in custom category.php
- Error trying import one category on page
- Remove the_content From Loop
- Display current category title on category page
- Installing wp3.2.1 on IIS; getting empty sessions
- Want to separate sections of posts. Can you restart loop?
- List all categories but exclude current post category on single post page
- Specific Loop For 2 Within Each
- Two posts in same div – WP loop
- Infinite-Scroll Plugin and Jetpack Infinite Scroll Plugin – Adding to “Thoughts” Theme
- Woocommerce, recognize the loop of related products
- Edit category output
- Easiest way to show total number of subpages
- Different background-image by category
- Output 2 items within the Loop
- Get posts with at least one category in common with current post?
- Custom excerpt function re-factoring
- How can I show the post thumbnail from the most recent of a certain post type, in widget?
- How can I add pagination and how can I change thumbnail size?
- Split loop into two columns, how to favor one side over the other
- Display post category in foreach loop with category link
- Show css depending on activity type in BuddyPress activity-loop [closed]
- how do I get a specific post from a post with a subcategory in WP
- WP grandchild categories in nested ul li
- How to pick the default selected value in wordpress dropdown?
- Get page that displays all children of taxonomy parent
- Counter in loop in foreach
- remove post that has no content
- Checking array against author id in loop
- How do I Turn This Into An if statement?
- How to show only subcategories in parent category not parent category?
- Widgets in the loop if active
- Dilemma of Populating all the categories in a drop down list
- Get post id within comments loop
- Syntax error when I try to insert my loop into an unordered list? [closed]
- JSON REST API WordPress only showing first 10 categories
- How to get post ID in a Page?
- PHP Warning: strip_tags() expects parameter 1 to be string?
- Echo a shortcode div after every 3 posts
- Get all categories post is not in
- How to overwrite orderby with add_query_var
- Get latest post from all categories except one
- Rewrite with pagination /foo/page/2/ to posts of a given category, page 2
- Only show first image in foreach loop
- How to pass current loop data into nested loop
- Post not populating for custom post type based on category selection
- Wpdb get->results to out the the month from the db
- Infinite Loop – WP_Query
- Removing sidebar on category pages
- Create Div Wrapper On 3rd Iteration Or When Query Loop Items Run Out
- go to home page when i select default in select-box
- How to create Loop code for Menu using WordPress?
- Wrong Number of Products When Hiding Subcategory Products From Showing In Parent Category
- How do I edit the terms output args or array data?
- Get main parent category for post (WordPress/Advanced Custom Fields)
- Display Visual Composer shortcode if a post belongs in specific categories
- How does Permalink work with the AMP plugin after Removing Parent Permalink Catalog from Posts
- ACF – Get ID of relationship field in flexible content
- How to output values from a loop into a javascript array
- WordPress Ajax filter: Create two loops for different output styles?
- How to display sticky post always at the top (before regular post) in wordpress?
- How to count posts posts if they are in a separate content page?
- Thumbnail wouldn’t scale to size properly
- Populate Posts based on category selected using AJAX that means without page refresh
- random woocommerce categories are not showing when count enabled?
- How to sort (orderby) a query done by a template function before the ‘foreach’ loop?
- How to Retrieve All Category Images on Front-End
- Add div after every 4 posts then every 2 posts for a responsive loop
- get_category display only 1
- Retrieving specific images from Media Library
- Custom Taxonomy Link Text Echo Name Not Slug
- How can I modify this code to iterate over an array of categories?
- Adding unique marketing messages between certain products whilst using isotope/Infinite Scroll
- I want my wordpress loop to only display posts of the standard format
- Make an array listing custom taxonomy
- Get category id for a custom category and display it in a class
- Automatically create a loop for post ID
- Is there a way to randomize and connect a background and header image?
- the_post_thumbnail unless video id is added
- dynamically filter by category via sub-menu
- Get unique post in parent category
- Incomplete term slugs output from a foreach loop with get_the_terms
- WordPress post pagination on custom template not working
- Implement “No related posts for this entry” into the loop?
- Excerpt all post content Content Same Size without word cutting off
- Options.php loop won’t show!