The core of the WordPress query, the loop offers an easy way to access post information and display it on your website.
10
votes
3answers
8k 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', ...
9
votes
3answers
610 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 ...
6
votes
1answer
117 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() ) : ...
5
votes
2answers
2k 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
325 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,
...
5
votes
2answers
793 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
535 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 ...
5
votes
2answers
4k 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 ...
4
votes
5answers
7k 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 ...
4
votes
1answer
2k 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 ...
4
votes
1answer
55 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
3answers
4k 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
1answer
186 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
348 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
1answer
285 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 ...
4
votes
1answer
125 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
72 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
2answers
974 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 ...
4
votes
3answers
275 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
1answer
463 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,
...
4
votes
2answers
431 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
252 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
2answers
79 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
5answers
5k 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 ...
3
votes
4answers
1k views
How can I create an alternative home page?
I'm working on a site which has a fully customized front page. Now I'm asked to add a more classic looking blog type page which will be reacheable at http://domain/blog.
I tried creating a custom ...
3
votes
1answer
646 views
How can I custom order the results from wp_list_categories?
I am using wp_list_categories to return (you guessed it) a list of categories within a custom taxonomy. The orderby parameter accepts sorting by ID, name, slug, count, and term_group. Is there a way ...
3
votes
2answers
826 views
How to display the_post_thumbnail if a post has one or otherwise display the first image in a post?
I would like to create a condition that checks to see if a post has a thumbail and if it does display it, otherwise display the first image in a post.
I tried something like this in my loop.php but ...
3
votes
1answer
638 views
How to add author details in the post sidebar?
I would like to add author details in the sidebar instead of bottom of the post?
Can anyone tell me how to do that?
As of now i'm using like this in the loop
<?php $curauthor = ...
3
votes
2answers
3k 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 ...
3
votes
2answers
225 views
Identify which loop you are hooking into; primary or secondary?
What is the most efficient way to determine which loop I'm in?
I have a few plugins that alter the query by hooking into various parts of WP_Query::get_posts(), through the usual suspects, ie ...
3
votes
4answers
3k 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 ...
3
votes
1answer
279 views
how do I group content in magazine-style 'issues'?
I am building a website for a magazine. It won't carry the whole content of the mag, just a teaser article or two from each issue.
I need to have a page for each issue that features these articles, ...
3
votes
1answer
150 views
Add 20yrs to post date, and then query
I would like to know if anyone knows a way of adding a timeframe (say 20 years) to the datestamp on a set of posts, and then running a query on the loop which uses the new date?
Basically what I want ...
3
votes
1answer
47 views
Wordpress “Loop” with large set of results
I'm not sure the appropriate terminology but I have a wordpress "loop" for a custom post type that has roughly 300 results that all get displayed on a single page (no paging). The causes an enormous ...
3
votes
1answer
57 views
How can I create a loop to build slides based on multiple categories using Coda Slider
I'm using a loop to generate the slides I need for a Coda Slider slideshow. The slides should be pulling the most recent post from 5 different categories. The problem I'm having is that it's pulling ...
3
votes
1answer
2k views
how to display full post with pagination on home page
I am using the 'Hatch' theme. I'm familiar with HTML/CSS but not with WordPress, first time working with this.
I have my homepage showing recent posts and that's good.
However, I want to make a ...
3
votes
1answer
129 views
How should I intercept the main query and inject custom join / order by / group by criteria
I have a CPT that is a member of two separate taxonomies. On the taxonomy archive page for one of the taxonomies, I need to further group and sort on the second taxonomy like so:
...
3
votes
1answer
246 views
Installing wp3.2.1 on IIS; getting empty sessions
i have to install wordpress 3.2.1 on IIS 5.0 (yeah, i know...) here. Brand new default wordpress installation, no plugins ore previous versions installed.
So, i downloaded the 3.2.1.zip and the ...
3
votes
2answers
2k views
Display list of Sub-Categories and the posts they contain, within one main Category
I've found tons of code and plugins to do various things; from show posts for specific cats, subcats of a cat, etc.. BUT, I cannot for the life of me find, nor do I know the WP API well enough to do ...
3
votes
1answer
92 views
Search widget breaks when using multiple loops?
I have created wordpress template that uses two loops via WP_Query object. Everything works fine except Search widget. I've been pulling my hairs out for three days now Googling like a madman,but ...
3
votes
2answers
107 views
How do I prevent one of two multiple loops from repeating on a second page?
I have a page with multiple loops, but the first loop repeats on the second (or Older) page. I want to keep the formatting but stop the post from repeating.
You can see the problem here: ...
3
votes
1answer
190 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 ...
3
votes
1answer
270 views
Creating nested forum loops in bbPress
I'm creating a customised forums front page for bbPress, by listing forums in a different way to the bbPress default.
Essentially what I'm trying to do is replace the use of bbp_list_forums() with a ...
2
votes
4answers
2k 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 ...
2
votes
2answers
2k 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' => ...
2
votes
2answers
6k 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.
2
votes
1answer
770 views
WP_Query vs get_posts
I have read a number of questions on SE regarding these but I still can't figure the exact difference wrt usage. Would it be true to say that I would probably use WP_Query for the majority of ...
2
votes
2answers
962 views
“pre_get_posts” firing on every query
How can I change arguments for the main query only, and not affect other queries?
add_filter('pre_get_posts', 'custom_post_count');
function custom_post_count($query){
...
2
votes
2answers
65 views
Get date of last update outside of loop
I'm trying to figure out how to display the date that a post was last updated outside of the loop. I'm able to display the published date using get_the_time() but there doesn't seem to be a "loopless" ...
2
votes
1answer
325 views
Is it necessary to reset the query after using get_posts()?
Is it necessary to reset the query after using get_posts() ?
I have been looking at this page and I don't see any reference to get_posts() ... I can't make it out for sure from this page either.
I ...