List All WordPress Posts by Category

If you are looking for a way to automatically generate a post index page, with all the post title & link in a WordPress website, the simple way is to use WP_Query, with “posts_per_page=-1” as $args.

There are ways to organize the list, for example, group the post by Category. In this case, we can add a new parameter in $args, which is “cat”, and put the query inside a category loop. Here are the code snippets.

Example 1: List all posts

 

Example 2: List all posts by category

 

I like tweaking script performance. Example 2 is straightforward using built-in WordPress functions, but not efficient in terms of performance. By interacting directly with Database, we only need to query one time. Here is another version to get all posts by category.

Example 3: List all posts by category

 

Comments are closed.

Post Navigation