{"id":23387242,"url":"https://github.com/wp-forge/wp-loop","last_synced_at":"2025-04-11T05:09:20.307Z","repository":{"id":57082022,"uuid":"278246786","full_name":"wp-forge/wp-loop","owner":"wp-forge","description":"A generator function that makes working with the WordPress loop a dream.","archived":false,"fork":false,"pushed_at":"2020-07-09T02:56:41.000Z","size":10,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T03:11:13.216Z","etag":null,"topics":["wordpress-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wp-forge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-09T02:52:26.000Z","updated_at":"2024-10-10T10:07:14.000Z","dependencies_parsed_at":"2022-08-24T14:58:18.533Z","dependency_job_id":null,"html_url":"https://github.com/wp-forge/wp-loop","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-forge%2Fwp-loop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-forge%2Fwp-loop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-forge%2Fwp-loop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-forge%2Fwp-loop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wp-forge","download_url":"https://codeload.github.com/wp-forge/wp-loop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247944365,"owners_count":21022571,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["wordpress-library"],"created_at":"2024-12-22T01:15:23.098Z","updated_at":"2025-04-11T05:09:20.284Z","avatar_url":"https://github.com/wp-forge.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WordPress Loop\n\nA generator function that makes working with the WordPress loop a dream.\n\n**When will this land in WordPress core?**\n\nAt the moment, that is unclear. However, you can follow the progress of this [trac ticket](https://core.trac.wordpress.org/ticket/48193). \n\n## Installation\n\n- Run `composer require wp-forge/wp-loop`\n- Make sure you require the `vendor/autoload.php` file in your project.\n\n## Usage\n\nHere are a few examples of how to use the `wp_loop()` function:\n\n### With the Global `WP_Query` Instance\n\n```php\n\u003c?php\n\nforeach ( wp_loop() as $post ) {\n\t?\u003e\n\t\u003carticle\u003e\n\t\t\u003ch1\u003e\u003c?php the_title(); ?\u003e\u003c/h1\u003e\n\t\t\u003cdiv\u003e\u003c?php the_content(); ?\u003e\u003c/div\u003e\n\t\u003c/article\u003e\n\t\u003c?php\n}\n```\n\n### With a Custom `WP_Query` Instance\n\n```php\n\u003c?php\n\n$query = new WP_Query( [ 'post_type' =\u003e 'post' ] );\n\nforeach ( wp_loop( $query ) as $post ) {\n\t?\u003e\n\t\u003carticle\u003e\n\t\t\u003ch1\u003e\u003c?php the_title(); ?\u003e\u003c/h1\u003e\n\t\t\u003cdiv\u003e\u003c?php the_content(); ?\u003e\u003c/div\u003e\n\t\u003c/article\u003e\n\t\u003c?php\n}\n```\n\nThere is no need to run `wp_reset_postdata()` after the loop. It is taken care of automatically, even if you break out of the loop early!\n\n### With an Array of `WP_Post` Objects\n\n```php\n\u003c?php\n\n$query = new WP_Query( [ 'post_type' =\u003e 'post' ] );\n$posts = $query-\u003eposts;\n\nforeach ( wp_loop( $posts ) as $post ) {\n\t?\u003e\n\t\u003carticle\u003e\n\t\t\u003ch1\u003e\u003c?php the_title(); ?\u003e\u003c/h1\u003e\n\t\t\u003cdiv\u003e\u003c?php the_content(); ?\u003e\u003c/div\u003e\n\t\u003c/article\u003e\n\t\u003c?php\n}\n```\n\n### With an Array of Post IDs\n\n```php\n\u003c?php\n\n$query = new WP_Query( [\n\t'post_type' =\u003e 'post',\n\t'fields'    =\u003e 'ids',\n] );\n\n$post_ids = $query-\u003eposts;\n\nforeach ( wp_loop( $post_ids ) as $post ) {\n\t?\u003e\n\t\u003carticle\u003e\n\t\t\u003ch1\u003e\u003c?php the_title(); ?\u003e\u003c/h1\u003e\n\t\t\u003cdiv\u003e\u003c?php the_content(); ?\u003e\u003c/div\u003e\n\t\u003c/article\u003e\n\t\u003c?php\n}\n```\n\n### With an Iterator\n\n```php\n\u003c?php\n\n$query    = new WP_Query( [ 'post_type' =\u003e 'post' ] );\n$iterator = new ArrayIterator( $query-\u003eposts );\n\nforeach ( wp_loop( $iterator ) as $post ) {\n\t?\u003e\n\t\u003carticle\u003e\n\t\t\u003ch1\u003e\u003c?php the_title(); ?\u003e\u003c/h1\u003e\n\t\t\u003cdiv\u003e\u003c?php the_content(); ?\u003e\u003c/div\u003e\n\t\u003c/article\u003e\n\t\u003c?php\n}\n```\n\n### Other Notes\n\nThe `wp_loop()` function is meant to be used in a `foreach` loop. If you need to check if there are results before looping, you can do that the way you normally would.\n\nFor example:\n\n```php\n\u003c?php\n\nif( have_posts() ) {\n    // For global query approach\n}\n\nif( $query-\u003ehave_posts() ) {\n    // For custom query approach\n}\n\nif( ! empty( $posts ) ) {\n    // For post or post ID approach\n}\n\nif( $iterator-\u003evalid() ) {\n    // For iterator approach\n}\n```\n\nThe `wp_loop()` function goes one step further than the standard WordPress loop does and automatically sets and restores the global `$post` object for each iteration.\n\nFor more details, read this blog post on [Creating a Better WordPress Loop](https://wpscholar.com/blog/creating-better-wordpress-loop/). The current implementation is a bit different, but the reasoning is laid out quite well.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwp-forge%2Fwp-loop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwp-forge%2Fwp-loop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwp-forge%2Fwp-loop/lists"}