As of May 31, 2023, we have updated our Code of Conduct.

Questions tagged [loop]

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

885 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
4 votes
1 answer
5k views

AJAX handler throws 400 (Bad request) - why?

I am trying to apply a "Load more posts" function to a post loop, but I am dealing with a 400 Bad request, when admin-ajax.php is referred to. The reference I used is this - https://rudrastyh.com/...
Dougless's user avatar
4 votes
1 answer
62 views

How do I get the total number of categories in a list of search results?

I want to display the total count of categories in a set of search results. For example, "10 posts found in 3 categories." (Assuming that each post has only 1 category.) Right now, my solution is to ...
Adam Pearlman's user avatar
4 votes
1 answer
2k views

Custom Loop and Infinite Scroll

I am using customized version of _s theme where I have Product CPT. In the following files, I need to use Infinite scroll (provided by Jetpack) - archive-products.php taxonomy-[tax-name].php ...
vajrasar's user avatar
  • 169
4 votes
0 answers
2k views

Loop that displays PARENT PAGE & CHILD PAGE & outputs GRANDCHILD PAGE title and content

I've never posted here, but I'm tired of slapping code together. That said, I would be most appreciative if someone could help me with this. I need a page template that: outputs the PARENT PAGEs, ...
user23213's user avatar
3 votes
0 answers
1k views

Show all parents and children in custom post type in right order

I would like to show all posts from a custom post type on one page with the ordering exactly like in the backend. I get the right ordering with wp_list_pages, but of course without the content. ...
Christopher Murphy's user avatar
3 votes
0 answers
62 views

Showing random content / pictures from earlier posts in a sticky post?

I have a sticky post on my wordpress blog, in the beginning. I would like to place some random content in it, by simply replacing some HTML in the post content. The random content would come from ...
Konstantin's user avatar
2 votes
0 answers
414 views

Why does using WP_Query inside a shortcode in an elementor page cause the arguments for WP_Query to get malformed?

I am trying to embed a custom WP_Query inside a shortcode I have activated on one of my pages (created in Elementor). I have a Custom Post Type called 'inspector-profiles', and for some reason when I ...
Barry Chapman's user avatar
2 votes
1 answer
24 views

Can't get custom posts of taxonomy to show

I have a custom post type called 'brand', with a taxonomy of 'brand-categories'. try as I might, I cannot find a way to display a list of posts in the current taxonomy. can anyone help? <?php ...
Captain Dando's user avatar
2 votes
2 answers
1k views

Show post page only if the user has bought a specific product

I'm having troubles with Woocommerce. I need to sell videos. I've created virtual products with a link to a different post for each (no download as I prefer to embed the video on my website and to use ...
Lia Muscogiuri's user avatar
2 votes
0 answers
198 views

Multiple WP_Query loops with Pagination Not Working

Please can anyone help me out here? I'm really stucked with this code. I want to create a multi post loop on my home page with each loop having its own pagination. The first loop is for recent post, ...
Moses Osasota Iribhogbe's user avatar
2 votes
0 answers
42 views

Fix inefficient loop breaks post.php on form submit

As is, this loop works fine. Although I feel like its inefficient because it doesnt need to keep updating the nonce counter each time it loops, that only needs to be done once. So when I try and move ...
jon jon bo fon's user avatar
2 votes
1 answer
568 views

wordpress post loop in javascript

How can I put WordPress post loop Codes in javascript? function top_news() { echo '<div class="news"></div>'; $query = get_posts(array( 'showposts' => 3, 'type' => 'topnews' ...
Tara's user avatar
  • 43
2 votes
1 answer
134 views

Category archive page - loop through posts of certain tag (with pagination) - pre_get_posts

I have a category archive page (press), which I need to loop through posts of a certain tag (magazines). I tried to modify the query with pre_get_posts, but it is still just showing posts in the '...
website walrus's user avatar
2 votes
0 answers
934 views

How to use max and min values of custom fields

How to use max and min values of custom fields (created via ACF, repeater type)? For axample, I've custom post type 'auto' and there are a lot of models. Each post(model) has few values of price (...
Влад's user avatar
2 votes
0 answers
53 views

How to iterate through database until it find a match

How can I loop through my Wordpress query, and check each posts if one meta_value of meta_key user_ip_reservering match the $ipadres Code $args = array( 'post_type' => 'reserveringen', )...
W. White's user avatar
  • 107
2 votes
1 answer
445 views

connect last post with first post

next/prev from my first to last and the other way around cant find a solution for that i am using this code for next <?php next_post_link('%link','<img src="img-location"/>'); ?> and ...
party mmr's user avatar
2 votes
5 answers
5k views

How to order posts by title after they have already been sorted by category

I'm not an expert at much, so please excuse me if this is an easy question... but it's got my head spinning. I have an archive template (adapted from a standard 'enfold' theme template) which I am ...
user110085's user avatar
2 votes
1 answer
359 views

Two loops by pre_get_post on same page

I have two loops in archive.php, both are affected by pre_get_post. My code on pre_get_post is this: // My pre_get_post function my_pre_get_posts( $query ) { if ( is_admin() || ! $query->...
Lucas Pulliese's user avatar
2 votes
1 answer
632 views

Double count view in archive.php

I'm having a hard time using this snippet I'm getting double views each time the post is queried. This is in my functions.php file: function getPostViews($postID){ $count_key = 'post_views_count';...
Al Rosado's user avatar
2 votes
1 answer
194 views

Lists Posts from CPT that match the Title of the Page

I am trying to get all the posts associated with the Title of a page. This is what I currently have: <?php $title_to_tag_posts_query = new WP_Query( array( 'tag' => ...
Nimara's user avatar
  • 161
2 votes
0 answers
1k views

Custom Loop through category menu to include sub categories

In my custom wordpress theme I currently have the below loop to the main categories <?php $menu = wp_get_nav_menu_object('navigation'); $items = wp_get_nav_menu_items($menu->term_id); foreach($...
odd_duck's user avatar
  • 123
2 votes
0 answers
60 views

Show top level comments ordered by number of replies

I am trying to modify the comment loop initiated by wp_list_comments to change the order they are returned. Specifically I have 2 levels enabled on my comments (top level and replies), and I want to ...
Jay West's user avatar
2 votes
0 answers
1k views

WP_Query Order by Specific Post ID First

I'm creating a website where I have a need to show a set of posts from a custom loop but I need to display a specific post first. This post will be dynamically specified in the URI by a $_GET ...
0Neji's user avatar
  • 203
2 votes
0 answers
222 views

Divide Loop Into Days & Categories

If you could help, I'd greatly appreciate it, because I'm halfway there, but I'm currently stuck! The site I'm working on needs to have it's posts, within the loop, divided up by days, and then under ...
Tyler Johnson's user avatar
2 votes
0 answers
2k views

multiple loops with pagination on the same page

I'm searching the web for this answer for three weeks now. Tried many things but nothing seems to work as how it should work. There must be a kind of trick what probably won't make sense to anybody ...
luukvhoudt's user avatar
2 votes
1 answer
411 views

Converting multiple loops into one single loop with pagination

I am using Isotope jquery plugin to filter/sort posts in Wordpress but to correctly filter the posts into the right order I have needed to output each category one after the other. To do this I've ...
egr103's user avatar
  • 305
2 votes
0 answers
96 views

Display posts with comments closed, with pagination?

i'm using a custom query, which needs to display all posts with closed comments. I have this <?php $the_query = new WP_Query('showposts=-1'); ?> <?php while ($the_query -> have_posts()) :...
idjuradj's user avatar
  • 185
2 votes
1 answer
349 views

Replace default Wordpress search behavior with Sphinx

I want to implement Sphinx Search in my Wordpress site but I'm not exactly sure how to change Wordpress search behavior to use Sphinx. How do I get Sphinx Search engine to replace whatever Wordpress ...
Tribbey's user avatar
  • 31
2 votes
2 answers
334 views

Display custom field of specific post where post title matches variable

I have a custom post type called vacancies and another called our_homes How do I get the google map coordinates from our_homes and display inside the vacancies single post template? My attempt below ...
Amesey's user avatar
  • 125
1 vote
1 answer
22 views

Complex Custom Loop with Includes

The Problem I have a complex loop designed for showcasing my portfolio. Here is the criteria: Portfolio is powered by a custom post type called PORTFOLIO Each post will be assigned a class of either '...
Sabotawsh's user avatar
1 vote
0 answers
86 views

WordPress Ajax load doesn't work

Hello I have set up custom Ajax load more without plugins, but it’s only showing 3 posts on home page, and when clicking on button to load more it’s not loading anything. As I am a beginner I am ...
Ferdal Karaman's user avatar
1 vote
1 answer
21 views

Prevent duplicating specific column from database table

I have SQL table of cities and counties. I need to loop over them and create dropdown with labels. Cities should be an <option> elements and counties wrapper <optgroup>s. function ...
Juraj's user avatar
  • 11
1 vote
0 answers
46 views

Wordpress Loop and $post

I decided to take a closer look at how WP works and ran into this question. I see that when WP is loaded, the wp() function is triggered, which will parse the request and get data from the database in ...
Alex's user avatar
  • 11
1 vote
0 answers
295 views

Compare ACF date field (if exists) and post date and order DESC

I have an ACF date field (same format as post date) and in my Query I want it to be compared to the publish date of the post: If erstelldatum is not empty use erstelldatum for order If erstelldatum ...
Omitheus's user avatar
1 vote
1 answer
48 views

WordPress Loop inside Loop?

I'm using ACF, and displaying the Brands CPT. Inside of brands, I will want to setup a separate loop for posts containing certain tags. My loop works, meaning that I'm able to access the brands and ...
Pbalazs89's user avatar
1 vote
0 answers
20 views

Sort by alphabetical order archive.php page

<?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?&...
Abir's user avatar
  • 11
1 vote
0 answers
57 views

Getting different functions data while using while loop in wordpress

I have added this code in the function.php file but when I use while loop in any other files this function is getting triggered. <?php add_action('loop_start', 'userlogin'); function userlogin() { ...
Nabeen's user avatar
  • 9
1 vote
0 answers
305 views

posts_per_page in custom WP_Query does not override "Reading" settings?

I am trying to use WP_Query loop (below) to show all instances of a certain post type. (This loop is not in the custom post type’s archive — it is in a template part that appears as a page section on ...
dubyaD's user avatar
  • 21
1 vote
1 answer
611 views

Group and list posts by custom taxonomy

I'm trying to list posts from a CPT named "exposition" and group them based on a custom taxonomy called "date-exposition" (which are meant to be years such as 2010, 2015, 2017,...) ...
TonioLip's user avatar
1 vote
0 answers
40 views

What is the recommended / best way to do this: Event calendar post/type in a block-based theme?

my question is this: assume I have a php-themed WordPress site with an event calendar page template that's using post type "event", which in turn has a lot of custom fields that hold the ...
yms's user avatar
  • 21
1 vote
0 answers
62 views

I write simple wordpress loop for taxonomy.php file but don't show result

I created a custom post type and create for CPT a taxonomy. but when I click on view taxonomy link don't work archive loop. I created taxonomy.php file for show this taxonomy in this file but display ...
Khaled Shokoohi's user avatar
1 vote
0 answers
28 views

Custom Tag Description unable to display just below and outside of the Loop

Hello StackExchange Team! In my wordpress tags for custom post type, the code <?php echo term_description();?> or outputs fine just outside and above the loop. The problem I am having is that ...
Okara Nnamdi Benedict's user avatar
1 vote
0 answers
24 views

Taxonomy name repeating when showing 2 posts from each category

I am trying to get my page to show a custom taxonomy and then a the first 2 posts in that taxonomy, My taxonomy is called types post type is called guides I want the page so it shows like this Type 1 ...
Chazlie's user avatar
  • 35
1 vote
1 answer
49 views

Why my filterable portfolio page work not perfectly between slug button and slug output WORDRPESS?

I currently have a Portfolio custom page in Wordpress, so far work as i want, but i have one problem. The problem is when i click one of the button slug then the body/output is not work like i want, ...
N. Arifin's user avatar
1 vote
1 answer
37 views

Exclude category from

I am trying to exclude a category from <?php the_category(', ') ?> inside my loop. I came across the following code on CSS tricks that says would solve the problem. You add this to the functions:...
Ed Manson's user avatar
1 vote
0 answers
669 views

Filter the loop by categories using checkbox form

I feel I'm close in solving this issue but kinda hit a wall. I have a loop on the examlpe.com/blog site and I'd like to be able to filter it using the category checkboxes. Here is my loop <h3 class=...
blackbear333's user avatar
1 vote
0 answers
54 views

Wordpress Loop - Next 3 Posts

Hoping someone here can lend me some advice or point me in the right direction. I have a working Wordpress php function where I can get the next 2 posts in the loop of the current post within a post ...
tractanz's user avatar
1 vote
1 answer
57 views

Missing image in gallery shortcode in custom feed

I have problem with custom gallery shortcode and missing first image in each gallery block. For example: [gallery ids="1,2,3,4,5"] show only 2,3,4,5 pictures. This custom shortcode is part ...
Nar's user avatar
  • 11
1 vote
0 answers
21 views

Change query based on post type while staying in loop

I have a loop that displays three custom post types. Thing is, all of these post types use a different logic. 'elemzesek' and 'gyorshirek' use ACF fields which you can see in the meta_query. However, ...
Pbalazs89's user avatar
1 vote
0 answers
58 views

CPT Loop with ACF and passing ID between Loops

I have a staging site at: https://tozsdeforum.ethermedia.hu/ Near the top of the page, we have a "Kiemelt Hirek" section. In this section, there are five loops in play: IF (pos1 == true) {...
Pbalazs89's user avatar

1
2 3 4 5
18