The core of the WordPress query, the loop offers an easy way to access post information and display it on your website.

learn more… | top users | synonyms (3)

21
votes
1answer
14k views

How to fix pagination for custom loops?

I have added a custom/secondary query to a template file/custom page template; how can I make WordPress use my custom query for pagination, instead of using the main query loop's pagination? Addendum ...
13
votes
4answers
14k views

Pagination not working with custom loop

I've got a custom loop that I'm using to display some Real Estate listings that will be available within 60 days. I'm calling it with the following function: <?php $sixtydays = date('Y/m/d', ...
11
votes
3answers
5k views

Why should I put if(have_posts()), is while(have_posts()) not enough?

I have a question about "the loop". <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your ...
10
votes
4answers
3k views

Display all posts starting with given letter?

I'm trying to build Wordpress based dictionary, basically it will have 26 pages (one for each letter): A B C ... X Y Z And every page will display all posts starting with given letter, so after ...
9
votes
1answer
108 views

Should we trust the post globals

@toscho left a comment to this answer which had me thinking again. How much trust should he have in the global scope, specially regarding the post globals like $post. So what? The global variable ...
7
votes
5answers
17k views

Get post content from outside the loop

Is there a way to get the content from another outside the loop? The ID is 302 and I need to display the content of that on another page.
7
votes
1answer
3k views

How can I fetch loop of post titles via AJAX?

I have a list of the most recent post titles in sidebar.php. Here is an example of how that code looks: <?php $args = array('posts_per_page' => 20); ?> <?php $sidebar = new ...
7
votes
3answers
225 views

Can I force WP_Query to return no results?

I'm working on a website with a search feature that allows users to search through a lot of post meta. There is a specific search pattern that I would like to forcibly return no results for. The ...
7
votes
3answers
752 views

Get 'page' number with infinite scroll

I added infinite scroll support of the Jetpack plugin to my own theme using this tutorial: add_theme_support( 'infinite-scroll', array( 'container' => 'content', 'footer' => false, ...
7
votes
2answers
2k views

How to check if I'm on the last page of posts?

I want to display some text on the last page that isn't displayed on the other pages. For example, on category pages: url.com/category/categoryname/page/6 or last page of all posts listed on the ...
7
votes
2answers
5k views

Getting only direct child pages in WordPress with get_pages

I am trying to fetch all direct children of a page. But I am getting all children and grand children as well. Any ideas? PHP Source: $args = array( 'child_of' => $post->ID, ...
6
votes
6answers
19k views

Get excerpt using get_the_excerpt outside a loop

I have a code that call get_the_title() and it works, but get_the_excerpt() return empty. How can i make it work? This code is inside a plugin called "WP Facebook Open Graph protocol". Here's the ...
6
votes
1answer
304 views

Why am I being limited to ten posts on a custom loop?

For example, I have a link on my site like /tag/green and select content from all posts tagged green are shown using a file loop-tag.php which is about as simple as <?php if ( have_posts() ) : ...
6
votes
1answer
6k views

How set featured posts using checkbox in post edit screen?

I want to make selecting a featured post is just by checking a checkbox in edit screen AND be able to retrieve these featured articles from only specific category ? in short what I'm looking for : ...
6
votes
2answers
650 views

Do we still need to include a “if (have_posts())” in templates?

I include this logic in my template all the time if (have_posts()): //show content else: //show content not found endif; But recently i began to doubt its necessarity, wordpress will turn to ...
6
votes
1answer
176 views

Inject post (from specific category) between posts in Loop

I would like to add a post (from specific category) between posts in the WordPress Loop (such as 'Sponsors'). Example: P P P S P P S P P S P P How can I achieve this? I am a beginner with coding, so ...
6
votes
3answers
5k views

Getting Post Comments for post ID using WP_Query() and a Custom Loop?

(Moderator's note: The original title was "Is there a way to get post comments by post ID on a custom loop using WP-Query()?") Hi! I'm running a custom loop using WP_Query, that only displays one ...
5
votes
5answers
13k views

How to split a loop into multiple columns

If I have a loop running from a category query like : <?php $the_query = new WP_Query('cat=1&showposts=50&orderby=title&order=asc');?> <ul> <?php while ...
5
votes
5answers
11k views

Display all posts in a custom post type, grouped by a custom taxonomy

I’m working on a member page where I use a custom post type with a custom taxonomy. My custom post type is called “member” and my custom taxonomy is called “member_groups”. I want to list all the ...
5
votes
2answers
3k views

How to add a “last” class to the last post in loop.php?

I need to add a "last" class to the last post that appears in loop.php. Can someone tell me how to accomplish this?
5
votes
3answers
2k views

Split Content and Gallery

Is there a way to split up the post content and the gallery short code. I want to display the gallery outside my normal content no matter how or where it is placed. I can use this to get the shortcode ...
5
votes
1answer
728 views

Cleanest Way to Select Every Second Element in a Loop?

What's the quickest and easiest way to select every second element in the loop? At the moment I'm using this strange method: <ul> <?php $k = 1; ?> <?php if (have_posts()) : while ...
5
votes
2answers
1k views

Usage of the new “posts_clauses” filter in Wordpress 3.1?

Just noticed that 3.1 added a new filter to customize queries: posts_clauses. All I could find about it is that instead of using the separate query filters like posts_where or posts_join, you can ...
5
votes
2answers
110 views

Retrieve each widget separately from a sidebar

I would like to retrieve each widget existing in a sidebar in order e.g. to mix them with posts inside the main loop. I know I can use different widget areas (sidebars) but this approach clutters the ...
5
votes
4answers
570 views

How to control output of custom post type without modifying theme?

I have a custom post type 'properties' that I'd like to control the HTML output for. For simplicity, let's focus on the archive view. As a basic example, here is what a loop looks like in an ...
4
votes
5answers
4k views

How to return results of a get_posts() in explicitly defined order

I'm trying to create a loop of explicity ordered posts, for example: <?php $args = array( 'include' => '1,3,8,4,12' ); ?> <?php get_posts( $args ); ?> The results are ...
4
votes
4answers
330 views

Why do themes rely on “The Loop”?

I've been looking at Wordpress themes development recently, and I'm trying to understand why Wordpress is coded with "The Loop", rather than providing an array of posts that you can loop through with ...
4
votes
1answer
18k views

Counting the posts of a custom Wordpress loop (WP_Query)?

I tried tplacing this: <?php $count = count($custom_posts); ?> <h2><?php echo $count; ?></h2> at the end of the loop: <?php if ( bbp_get_forum_title() == ...
4
votes
1answer
60 views

Why doesn't /2013/01/ properly return January's archives in archive.php?

I've figured out how to implement pagination into WordPress archives, etc.., but the content isn't being returned correctly. Instead of /2013/01/ returning January 2013's archives, it instead returns ...
4
votes
2answers
414 views

Do I need to use The Loop on pages?

I'm writing my first WordPress theme and have a question about the use of The Loop in page templates. The page templates I've looked at all follow basically the same pattern (example from Twenty ...
4
votes
2answers
55 views

How to force excerpts / teasers in the loop

When I'm in "the loop" (archive pages etc.) how do I force teasers / excerpts for all articles, regardless of whether they contain the <!--more--> tag or not? Details: I'm starting off of the ...
4
votes
2answers
1k views

Resetting post data to previous loop in nested loops

I'm trying to use nested loops with the posts to posts plugin. The loops both work, but the problem arises after the second nested loop ($issue). I want to access the $publication loop again, but the ...
4
votes
3answers
6k views

Modify main WordPress loop with a parse_query filter

I am trying to modify the main WordPress loop on my category pages like so: add_filter('parse_query', 'my_modified_query'); function my_modified_query( $q ) { if (!is_admin() && ...
4
votes
4answers
6k views

How do I exclude a custom taxonomy from the post loop

Is there a simple or easy way to exclude all posts from a custom taxonomy in the loop? I've been looking high and low, and neither SE, SO or Google seem to have a straight answer. I know it can be ...
4
votes
1answer
328 views

How to return loop contents

At times, I need to return the output of a loop (usually with WP_Query like in this example) for use in a shortcode or with a filter on the_content. The following code that uses object buffering ...
4
votes
1answer
535 views

How-to exclude terms from the main query the most performant way?

This Q is a follow up to this answer on the Q: "How to exclude a specific term for the search?". 4 ways to filter out posts that have a specific term Type | Pro | ...
4
votes
2answers
5k views

Custom Post Type Archive Page: Set Posts Per Page, Paginate

I have a custom post type called video. Would like to paginate its archive page, showing only 3 posts on each page. There is also a custom loop on the archive page that outputs all the video posts ...
4
votes
3answers
176 views

Skip post in loop and mixin later

In the blog, i try to avoid to show two posts from the same category in succession. So if the latest two posts are from category 1, i want to skip the second one, take post 3 first (if it's not from ...
4
votes
1answer
158 views

How to show Y number of custom posts after every X normal posts?

I have a loop within a loop. I figured out how to show all or X number of custom posts after Y number of normal posts. I also figured out how to show 1 custom post after every Y normal posts. How do I ...
4
votes
1answer
338 views

How can I save an array from a random post sequence for later use?

I'm building a random workout generator on Wordpress to help with my fitness, and am stuck at saving random post arrays for later reference. I'll explain. The generator should work in two main ...
4
votes
3answers
163 views

Avoiding using a loop to access a single post

Sometimes I want to access one particular CPT to extract something from it, for example a custom field value: $group = new WP_Query( array( 'post_type' => 'group', 'p' => $group_id ) ...
4
votes
1answer
85 views

How to place a loop within another loop?

I have two loops setup using WP_Query: $latest and $popular that I'm working to setup in this example layout: Latest                        ...
4
votes
1answer
221 views

Category as Class for Custom Post Type

I've only just realised that custom post types don't appear to output the category as a class in the loop, via post_class and I wonder if anyone can suggest a work around. Is this something that ...
4
votes
2answers
202 views

Rearranging posts based on categories

Hey everyone on Stackexchange! I'm trying to find a way to prioritize posts based on a category in a loop, which is sorted by dates. All posts are grouped under their respective dates. The date ...
4
votes
2answers
829 views

Build a content and excerpt grid loop with paging and options for # of posts

What I'd like to do: on index.php, a loop that shows a selectable number of full posts using the_content and then below that shows a selectable number (the number of posts can be hardcoded in the ...
4
votes
2answers
502 views

Loop.php vs looping inside template file

What is best practice for WP with regard to using loop.php (loop-single.php, etc) versus looping inside the template file? Does it matter with regard to efficiency or ?
4
votes
1answer
554 views

How do you get the count of posts in an archive page?

I have a custom posts archive page of the type archive-my_custom_post_type.php and I am using the "standard" loop which consists of the ever-so famous <?php if(have_posts()) : ?> <?php ...
4
votes
1answer
479 views

How to show list of posts by author and category?

Let three categories and their IDs: cat1 = 1 (parent) cat2 = 2 (child of cat1, parent of cat3) cat3 = 3 (child of cat2) Let each category has 3 posts, they are: post1, post2, post3. And each posts ...
4
votes
2answers
114 views

Randomise results from a category page?

I have my standard category page. On this page, I'd like to put a button which, when clicked, randomises the results shown on the category page. I know I could make a rand-category.php page and send ...
3
votes
2answers
4k views

WP_Query and next_posts_link

I cannot figure out how to make next_posts_link() work within my custom WP_Query. Here is the function: function artists() { echo '<div id="artists">'; $args = array( 'post_type' => ...