Your examples are showing two different thing The first one is looping over categories and the second is looping over posts, so the answer in this case is simple and depends on what you need to loop over (posts or categories).
No if you meant when should you use query_posts()
vs. get_posts()
vs. WP_Query
then take a look at this awesome answer
.
Related Posts:
- Static Front-Page Excerpts
- How do I query based on the modified date?
- WordPress loop problem: Multiple loops, index.php and is_paged causing duplicate posts on next page
- How do I query by post format in WordPress 3.1
- How to return results of a get_posts() in explicitly defined order
- Modify main WordPress loop with a parse_query filter
- How should I intercept the main query and inject custom join / order by / group by criteria
- query_posts sort in multiple directions
- Dynamically excluding current page id
- How to order results by date and meta key?
- Adding Variables to post query
- Is it possible to query_posts using post__in and then Loop through them in the ordered they were queried?
- How to insert content from another Custom Post type into Post?
- Placing post data (title, date, and excerpt) nested in static content
- query_posts() ALWAYS displays something?
- How to remove a plugin filter’s priority on specific loops (custom queries)?
- Custom loops, sticky posts, and pagination nightmare
- Filter Loop by Custom Field Value
- Modify loop but keep the original query, what am I doing wrong?
- Post per page not working if no category specified
- Calling Posts from Specified Cat
- Magazine style frontpage with multiple categories/loops and no duplicate posts
- Most Effective Method? Exclude Category and Number of Posts Per Page
- Script that gets the first tag of post and query all other posts with the same tag
- How to add an “or” instead of and “and” (&) in a WordPress query?
- Query add html after set amount of posts?
- Modify main loop query for paged and meta key
- Create a Loop with Posts Ordered by Most Recent Comments
- query_posts and sub pages?
- The_content display a different content from a previous loop
- Including post data in a sidebar occurring outside of and before the Loop
- Multiple loops with / without sticky posts and different post limits
- Limitless amount of posts in custom archive page
- How to Fix an Archive.php That Displays All Posts?
- Add Class according to the order of appearance
- query_posts orderby multiple ways
- Optimize CPT-function with a loop
- Display specific posts on home page
- Make query_post category name dynamic
- How can I list posts with different formats depending on order?
- query_posts orderby postmeta [closed]
- Can I use WP_Query one time and then filter the results multiple times
- pagination broken – clicking next displays “page not found”
- Multiple instances of query_posts
- WordPress Query based on random taxonomy
- Paginate pages with dynamic query
- Displaying and Querying Posts with Featured image
- Pagination linking to first page
- List post only under the category, exclude child category content
- Restrict query_posts by Date?
- Different layout based on post amount?
- How to break up php code to avoid echo
- Query Multiple Custom Post Types & Exclude a Taxonomy Term
- Custom Post type showing up in loop, regular posts are not showing up
- Posts being viewed
- How to solve this without flushing the rewrite rules for each post query the visitor triggers?
- Display 3 posts with different HTML markup using a loop
- View Achive with Category Filter, but Page per post not working
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Determine if ID is page or post and query the ID
- Pagination for two loops
- Custom template for password protected page
- Sort post by custom field numeric value
- Meta query stopped working
- If Query Post Returns 1 Post
- Custom Post Type Single Loop Outputting Wrong Post
- Weird problem on if statement
- Sticky posts don’t show up first in custom loop
- Exclude current post ID from loop in sidepbar.php
- How to check a list or feed of all posts under a category and tag?
- Want Page Descendants when using query_posts post_parent or child_of
- grab neighboring content in a query
- Issue with sidebar widgets not showing when I do custom queries
- Why would post_type be ignored in this query?
- query_posts call sets is_home() to true
- show all in query_posts on category.php?
- Notting but the latest post content keeps on being loaded
- query_posts doesnt show pages with given ID when post_type => page
- Query Posts Incorrectly Displaying Tags Archives
- Keeping get_posts’ offset and is_paged() synchronized
- Custom Post Type Query W/Category Dropdown
- Querying a query
- Best Query for blog posts
- odd behaviours from template tags
- Having an issue doing a WP_Query with post_content and category__and
- Trouble highlighting correct menu parent with wp_nav_menu classes while viewing “single posts”
- Custom query looking at multiple custom fields and properly sorting
- How do I add a timezone offset to this query?
- query_posts thumbnail condition
- How to put posts in pages using query posts
- Show posts from specific roles
- Latest 5 post excerpts from 5 different authors in Sidebar
- post var problem with shortcode loop
- Instead of 4 loops, how can I do it better?
- Custom Post Types Not Showing Up In query_posts Result
- Exclude current post but keep the same posts-per_page amount
- Custom loop off by one post
- Pre-existing arguments when using queries
- Problem with WP_Query loop and search term
- Is there any way of only retrieving posts with one or more thumbs up (GD star rating plugin)?