You should read the function reference for WP_Query on the WordPress codex. There you have a lot of examples to look at. If you don’t want to loop over the result set using a while
, you could get all posts returned by the query with the WP_Query
in the property posts
.
For example
$query = new WP_Query( array( 'post_type' => 'page' ) );
$posts = $query->posts;
foreach($posts as $post) {
// Do your stuff, e.g.
// echo $post->post_name;
}
Related Posts:
- Pagination with custom SQL query
- How to list some posts first in the loop based on post id
- Different ‘posts_per_page’ setting for first, and rest of the paginated pages?
- Add indexing to meta_value in wp_postmeta
- Custom WP_Query order by post_meta and (author) user_meta
- WordPress Custom Query to show posts from last x years
- How to order posts tag by tag?
- Want to redirect if search query match exact title of any post
- Is there an action/filter called when WP_Query->query is finished and assigns posts
- get images attached to post
- Most commented post should be the first post in the blog
- How to support multiple search terms query within one process?
- get data from wp-query, outside the loop & without url change
- Search another database with wp_query using new wpdb
- Custom page template query_vars
- Order by the results of a function with WP_query
- Pagination not working for category posts
- Querying by taxonomy vs Querying by Custom fields Speed Comparison
- WP_Query class not found
- How to use WP_Query to filter posts modified after a specific date
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Include Sticky Posts with Custom Query
- Order WP Query posts by custom order calculated from post meta values
- How to query for custom field within Gutenberg block from outside of that post? [closed]
- WP_Query mixe post and page attributes
- Wp query orderby ‘title’ doesn’t work
- How do you Query posts with nothing in common?
- Exhausted memory limit with very simple WP_Query
- Get meta info related to current post
- When to add_filter() to Custom Query
- if/else on custom query gives 200 OK when condition not met?
- Creating arguments from loop for WP_Query meta_query
- Select from wp_post and multiple meta_value from wp_postmeta
- Multiple Loops Inside a Function
- Store sticky post’s ids in a transient
- How to use MySQL’s MATCH AGAINST in WP_Query?
- Ajax Pagination on Ajax filter
- Wp_query returning only one post while query through ajax
- Woocommerce query all products and categories
- Add “Category” to Mysql Query
- Can I set my WP_Query to be a Main Query?
- How to use external API content in place of WP posts using $wp_query?
- Make a page return false to is_single() and true for is_post_type_archive() before wp_enqueue_scripts
- category query for pages not working
- Is it possible to set specific posts to show first in a query?
- date_query on draft posts only
- wp_query args adding muitiple tax_querys
- How to get list of posts from permalinks?
- wp_query not working with post_type
- WP_Query posts with comments only
- Default permalink structure causing Notice: Undefined property: WP_Query::$post
- How to check the array values, what WP_Query has brought to me?
- How to Order a list of taxonomies? orderby?
- Get posts that do not have the same tags as current
- queried_object using pre_get_posts gets notices and warning
- post_parent don’t work and return 0 page
- date_query in pre_get_posts out of memory
- Exclude a Woocommerce product from WP_Query
- WP_QUERY wrong ammount of posts
- Expecting statement error from php loop using ACF plugin
- Get specific ACF key and value from all posts – no access to DB
- How do I sort this custom list of sticky posts
- Query only displays one page_id
- How to exclude a category name from showing?
- All Posts Have The Same Comments
- Trying to get property of non-object in: $wp_query
- How to set parameters for search loop?
- I am officially missing something about transient posts
- Register Taxonomy – What is `query_var`?
- Get Post ID as a separate RSS feed item
- how to retrieve WP_Query without ordering by date [duplicate]
- Which is from this queries is more faster
- Related posts by current posts child category
- Block internal search queries with pre_get_posts and regex rules
- Is it possible to give a classname to specific comments in the WordPress admin?
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- Is there documentation for objects returned by WP_Query?
- How to upload 3 attachments to current post?
- Custom post type loop error: Trying to get property of non-object
- Search Field that allows options of “Match any keyword” or “Match all keywords”
- WP_Query for posts that have postmeta assigned to a taxonomy
- Getting the last X posts, but in ascending order of time
- Help displaying related categories
- Show more than one post on the main page excluding certain category
- How to make a wordpress loop file that displays posts based on certain conditions
- WP_Query with MetaQuery issue
- Custom loop – Isolating post meta output depending on current query taxonomy terms
- Comment count same for every post in homepage WP_Query
- How do I display posts with specific value in a custom field into my loop?
- Orderby Meta Value and Query from Meta Query
- WP Query to order posts by multiple meta fields
- Order by multiple meta keys on wordpress
- Sort posts in dashboard using custom field; also include posts where field isn’t set
- paginate_links() with Custom Taxonomy
- Output ACF field dynamicaly within a taxonomy loop [closed]
- WP query with multiple custom meta not respecting orderby
- 1500+ duplicate queries via get_option function (query monitor)
- AJAX search function resets wp_query vars
- I need to get all categories from a WP_Query
- WordPress Ajax search filter on dropdown select