Offset posts from random loop

Posted on 16th September, 2017 144 Comments

I recently created a post grid with 3 post loops of random posts, separated by a single meta field content after each iteration.

This is where wp_list_pluck came into its own. You use the previous query array to offset the random posts used in the previous loop.

to offset the previous posts include the post__not_in to the WP_Query array.

The first loop was a standard loop, but the second and third required the removal.
The first loop:

$first_grid_query = new WP_Query(
	array(
		'posts_per_page'=>2,
		'post_type' => 'casestudy',
		'orderby' => 'rand',
	)
);
while ($first_grid_query->have_posts()) : $first_grid_query->the_post(); 

The second itteration looked like this:

$first_grid_posts_ids = wp_list_pluck( $first_grid_query->posts, 'ID' ); 

$second_grid_query = new WP_Query(
	array(
		'posts_per_page'=>3,
		'post_type' => 'casestudy',
		'orderby' => 'rand',
		// Add the post ids from the first loop
		'post__not_in' => $first_grid_posts_ids,
	)
);
while ($second_grid_query->have_posts()) : $second_grid_query->the_post(); 

The second required an array merge, to include the first posts + second loops post.

// grab the IDs from the above query
$second_grid_posts_ids = wp_list_pluck( $second_grid_query->posts, 'ID' ); 
// Merge the 2 queries into a single array
$merged_post_id_arrays = array_merge($first_grid_posts_ids, $second_grid_posts_ids);

$third_grid_query = new WP_Query(
	array(
		'posts_per_page'=>1,
		'post_type' => 'casestudy',
		'orderby' => 'rand',
		// remove merged arrays from this loop
		'post__not_in' => $merged_post_id_arrays, 
	)
);

while ($third_grid_query->have_posts()) : $third_grid_query->the_post(); 

Comments

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]

JamesVen

21st, Oct, 20

Abofqn kdimok [url=https://ciamedusa.com/]cialis prices[/url] and bar universal blueprint nearly the same are what you.

JamesVen

21st, Oct, 20

wait, below cost cialis online canadian apothecary, whatever channel, antagonism. [url=https://ciamedusa.com/#]cheap cialis tablet[/url] a reduced without the climb cialis online shoddy a pressure.

RobertBAR

31st, Oct, 20

erectile restoration centers llc [url=http://erectiledysfunctionpillscvs.com/#]erectile pills over the counter[/url] erectile sonic

RobertBAR

05th, Nov, 20

buy erectile dysfunction medications online [url=http://erectiledysfunctionpillscvs.com/#]erectile pills[/url] erectile medicine

Stevewah

08th, Nov, 20

[url=https://sittindonesia.org/d6967fe9748d6dc60a6682f497bcfd1c/book/1603726226-9783741153525]Das Albanesische Element in Griechenland[/url]

[url=https://serpillo.it/fc96fad52c0d859b1f88f6622317530d/magazine/unique-homes-winter-2015]Unique Homes – Winter 2015]

[url=https://mayxaydungsg.com/en/b2f7242ec1e43289269a9e92c73be0ae/stream/60971-1604366758/1996]Critics’ Choice Movie Awards[/url]

[url=https://alufiero.it/en/f4e82d07710d7fc5bb4b7ba00308a0ae/stream/385448-1603592896/2018]The Little Mermaid]

WWW.XMC.PL

10th, Nov, 20

It is incredibly, very user-friendly and user friendly. Great job for the fabulous site.

Hello boy! you wanna see more nudes? Check my videos for link! https://asya.page.link/TH7z

11th, Nov, 20

intimate here are my photos. Nick Dipassy69/ You are very beautiful and hot, fuck me in all holes)
https://doroty.page.link/JRiW

Cissr

12th, Nov, 20

USA
cialis generic equivalent [url=https://cciialiss.com/]buy cialis brand[/url] buying cialis dubai

Cissr

12th, Nov, 20

Generic
effective cialis dose [url=https://cciialiss.com/]cialis without prescriptions[/url] how can i buy cialis without a prescription

Cissr

12th, Nov, 20

USA
cialis bathtub meaning [url=https://cciialiss.com/]buy cialis in usa[/url] want buy cialis

Cissr

13th, Nov, 20

Brand
cialis every day dose [url=https://cciialiss.com/]generic cialis[/url] best cialis buy online

Cissr

13th, Nov, 20

Brand
exactly does cialis do [url=https://cciialiss.com/#]buy online[/url] cialis 20mg review

Cciialiss

13th, Nov, 20

Generic
is generic cialis real [url=https://cciialiss.com/]no prescription cialis[/url] cialis loses effectiveness

Cciialiss

13th, Nov, 20

Generic
cheapest way get cialis [url=https://cciialiss.com/#]cciialiss[/url] cialis harmful liver

Cciialiss

13th, Nov, 20

Brand
natural replacement cialis [url=https://cciialiss.com/]cialis pills[/url] buy cialis with no prescription

Cciialiss

13th, Nov, 20

Generic
cialis 5mg for women [url=https://cciialiss.com/]cialis 20mg[/url] cialis cash price

Cciialiss

13th, Nov, 20

Generic
cialis 30 mg [url=https://cciialiss.com/]buy cialis overnight[/url] generic cialis eu

Cciialiss

13th, Nov, 20

Generic
cialis generic capsules [url=https://cciialiss.com/]buy cialis usa[/url] buy cialis in usa

Cciialiss

14th, Nov, 20

USA
cialis cure ed [url=https://cciialiss.com/]buy cialis brand[/url] buying cialis nz

Cciialiss

14th, Nov, 20

Brand
cialis professional overnight [url=https://cciialiss.com/]best generic cialis[/url] cialis 80 mg

Cciialiss

14th, Nov, 20

USA
cialis how long before [url=https://cciialiss.com/]cialis online[/url] cialis daily use discount

Cciialiss

14th, Nov, 20

USA
get best effect cialis [url=https://cciialiss.com/#]buy online[/url] cialis right dose

Cciialiss

14th, Nov, 20

Brand
buy cialis online us [url=https://cciialiss.com/]generic cialis buy[/url] best generic pharmacy cialis

Cciialiss

14th, Nov, 20

Brand
generic name for cialis [url=https://cciialiss.com/]buy cialis 40 mg[/url] cialis users experience

Cciialiss

14th, Nov, 20

Generic
cialis online uk [url=https://cciialiss.com/]buy cialis canada[/url] preis cialis levitra

Cciialiss

15th, Nov, 20

USA
cialis meaning [url=https://cciialiss.com/]cialis without prescriptions[/url] cialis pill effects

Cciialiss

15th, Nov, 20

Generic
cialis use [url=https://cciialiss.com/]buy cialis online[/url] cialis look like

Cciialiss

15th, Nov, 20

Generic
cialis cheap india [url=https://cciialiss.com/]generic cialis buy[/url] many doses cialis

Cciialiss

15th, Nov, 20

Brand
cialis internet pharmacy [url=https://cciialiss.com/#]cciialiss com[/url] cialis in italien

Cciialiss

15th, Nov, 20

Brand
lowest dose cialis [url=https://cciialiss.com/]buy cialis canada[/url] cialis next day

Cciialiss

15th, Nov, 20

USA
cialis generic effectiveness [url=https://cciialiss.com/]buy cialis[/url] does generic cialis really work

Cciialiss

15th, Nov, 20

Generic
cialis costco [url=https://cciialiss.com/]cialis pills[/url] cialis 80 mg price

Cciialiss

15th, Nov, 20

Generic
cialis not needed [url=https://cciialiss.com/]best generic cialis[/url] cialis discounts

Cciialiss

15th, Nov, 20

USA
ok break cialis half [url=https://cciialiss.com/]buy cialis usa[/url] cialis effects in women

Cciialiss

16th, Nov, 20

Brand
mail order generic cialis [url=https://cciialiss.com/]buy cialis canada[/url] long effects cialis last

Cciialiss

16th, Nov, 20

Brand
cialis generic effective [url=https://cciialiss.com/]buy cialis in australia[/url] cialis product label

Cciialiss

16th, Nov, 20

Brand
buy generic cialis pills [url=https://cciialiss.com/#]buy online[/url] generic cialis pharmacy

Cciialiss

16th, Nov, 20

Generic
date validite cialis [url=https://cciialiss.com/]cialis 20mg[/url] best use of cialis

ccialisny

17th, Nov, 20

online rx for cialis [url=https://ccialisny.com/]mail order and cialis[/url] 30 mg of cialis

ccialisny

17th, Nov, 20

cialis best price [url=https://ccialisny.com/]cialis ordering[/url] buy generic cialis eu

ccialisny

18th, Nov, 20

cialis 5 mg n1 [url=https://ccialisny.com/]order cialis 20 online[/url] benefits of using cialis

ccialisny

18th, Nov, 20

cialis 5mg uk [url=https://ccialisny.com/]order cialis 20 online[/url] cialis online usa

ccialisny

18th, Nov, 20

best deal on cialis [url=https://ccialisny.com/]order cheap cialis online[/url] cialis long term effects

llevitraaa

19th, Nov, 20

levitra 10 mg schmelztabletten ohne rezept [url=https://llevitraa.com/]levitra buy online cheap[/url] efek samping viagra cialis levitra

llevitraaa

19th, Nov, 20

levitra synthesis [url=https://llevitraa.com/]levitra buying[/url] levitra for prostate problems

llevitraaa

19th, Nov, 20

kas geriau viagra cialis or levitra [url=https://llevitraa.com/]drug levitra sale[/url] como usar levitra 10mg

llevitraaa

20th, Nov, 20

levitra and multiple myeloma [url=https://llevitraa.com/]levitra buy in uk online[/url] a que edad se puede tomar levitra

llevitraaa

20th, Nov, 20

does levitra cause headaches [url=https://llevitraa.com/]buy levitra[/url] levitra cialis combination

Andrewreigo

20th, Nov, 20

male potency pills [url=https://hipotencyrx.com/]potency pills[/url] brand and generic in best prices with fast shipping!

llevitraaa

20th, Nov, 20

existe generico de levitra [url=https://llevitraa.com/]online levitra buy[/url] acheter levitra 5mg

llevitraaa

20th, Nov, 20

best results with levitra [url=https://llevitraa.com/]generic levitra buy australia[/url] que efectos tiene levitra

llevitraaa

20th, Nov, 20

nebenwirkungen viagra cialis levitra [url=https://llevitraa.com/]online levitra buy[/url] levitra ratings reviews

llevitraaa

21st, Nov, 20

comparativa viagra levitra cialis [url=https://llevitraa.com/]generic levitra online pharmacy[/url] levitra orosolubile da 10 mg

llevitraaa

21st, Nov, 20

levitra 10 mg generikum [url=https://llevitraa.com/]levitra buying[/url] levitra odt apteka

llevitraaa

21st, Nov, 20

levitra e occhi rossi [url=https://llevitraa.com/]generic levitra buy india[/url] dose de levitra

fixcialel

24th, Nov, 20

best generic drugs cialis [url=https://cialisfix.com/]cheap cialis online pharmacy[/url] tadalafil sublingual tablets

fixcialel

24th, Nov, 20

cialis normal dosage [url=https://cialisfix.com/]walmart pharmacy cialis[/url] cialis max dose

fixcialel

25th, Nov, 20

can i buy cialis in mexico [url=https://cialisfix.com/]cialis mail order pharmacy[/url] high dose cialis

fixcialel

25th, Nov, 20

cialis overdose results [url=https://cialisfix.com/]does cialis help prostatitis[/url] levitra vs cialis review

fixcialel

25th, Nov, 20

manufacturer of cialis [url=https://cialisfix.com/]does lisinopril interact with cialis[/url] cialis new zealand

peera

26th, Nov, 20

[url=https://курган.товаромания.рф/reviews.php]Отзывы[/url]

DonPrisa

28th, Nov, 20

Новый год уже близко.
В интернет магазине Товаромания.РФ снижены цены.
Бесплатная доставка.
Самый выгодные предложения на [url=http://xn--80aae0ashccrq6m.xn--p1ai/index.php?cat=815]Дома и замки[/url] только сейчас.

ciialel

28th, Nov, 20

cialis 5mg daily price [url=https://cciialiis.com/]cciialiis.com[/url] cialis good prostate

ciialel

28th, Nov, 20

cialis how long last [url=https://cciialiis.com/]cialis generic buy[/url] cialis brand vs generic

SmmPEn

28th, Nov, 20

Отложенный постинг в инстаграм [url=https://postingall.ru/ ]и в остальные соц сети Telegram Прокси и другие мощные инструменты[/url]

ciialel

28th, Nov, 20

tadalafil women [url=https://cciialiis.com/]walmart pharmacy cialis[/url] cialis boost

KennethHeedo

29th, Nov, 20

+ за пост
_________________
[URL=https://polskiekasyna.realmoneygame.xyz/2874/]restauracja kasyno wojskowe rembertow[/URL]

ciialel

29th, Nov, 20

is generic cialis legitimate [url=https://cciialiis.com/]cialis mail order australia[/url] cialis lipitor

ciialel

29th, Nov, 20

original cialis [url=https://cciialiis.com/]mail order and cialis[/url] tadalafil prices

Johnnyzef

29th, Nov, 20

Спасибо за пост
_________________
[URL=https://tr.onlinerealmoneytopgame.xyz/1119/]tjk izmir tahmin[/URL]

Cciialiss

30th, Nov, 20

Brand
cialis or levitra [url=https://cciialiss.com/]order cialis miami[/url] cialis 5 mg colombia

Cciialiss

30th, Nov, 20

Generic
buy cialis online nz [url=https://cciialiss.com/]order cialis online[/url] levitra vs cialis effectiveness

JamesBup

30th, Nov, 20

САмое эффективное для продаж – Pinterest. Dbltj – Сотни Продаж на Etsy, amazon, ebay, shopify за 2 месяца при ср.цене чека 300 usd https://youtu.be/GNOZtXGGM-I

urgepay

30th, Nov, 20

cialis prescription Adereedymn [url=https://dcialish.com/]canadian pharmacy cialis 20mg[/url] stumbasy Priligy 2011

Cciialiss

30th, Nov, 20

Brand
purchase real cialis [url=https://cciialiss.com/]order cialis with dapoxtine[/url] buy cheap cialis

Cciialiss

30th, Nov, 20

USA
efficacy cialis 5mg [url=https://cciialiss.com/]cialis mail order usa[/url] cialis powder form

Cciialiss

30th, Nov, 20

Brand
cialis cheap usa [url=https://cciialiss.com/]cialis order online[/url] bestil cialis

Cciialiss

01st, Dec, 20

USA
buy cialis in vancouver [url=https://cciialiss.com/]order cialis brand[/url] cialis online problems

Grach

01st, Dec, 20

Новый год уже близко.
В интернет магазине Товаромания.РФ снижены цены.
Бесплатная доставка.
Самый выгодные предложения на [url=http://xn--80aae0ashccrq6m.xn--p1ai/index.php?cat=4324]Уральский ФО[/url] только сейчас.

Cciialiss

01st, Dec, 20

Generic
cialis daily use buy [url=https://cciialiss.com/]order cialis 20 mg[/url] generic cialis australia

Cciialiss

01st, Dec, 20

USA
best way get cialis [url=https://cciialiss.com/]cialis mail order usa[/url] cialis price compare

LeonardZed

01st, Dec, 20

САмое эффективное для продаж – Pinterest. Смотрите Видео пример продаж: Сотни Продаж на Etsy, amazon, ebay, shopify за 2 месяца при ср.цене чека 300 usd https://youtu.be/GNOZtXGGM-I

BradleyNug

03rd, Dec, 20

Viagra: $0.31
Cialis: $0.69
Kamagra: $0.90

[b][url=https://bit.ly/3ktuIQ6]Reputable Online Pharmacy[/url][/b]

+ 100% Confidential and anonymous
+ New FDA Approved Drugs
+ Special internet prices and discounts
+ Brand and generic RX meds
+ Fast worldwide delivery

List of Accredited Canadian Online Pharmacies:
[b][url=https://bit.ly/3ktuIQ6] + Check Your Online Pharmacy[/url][/b]

How to alleviate Lady era side effects naturally? Reviews of Lady era from India. [url=https://pharmacies.weebly.com/]Canadian Pharmacy[/url]. Order Lady era side effects in males.
Customer reviews of Lady era. [url=http://dorehami.tavanafestival.com/showthread.php?tid=10000&pid=10985#pid10985]Canadian Online Pharmacy[/url] Buy generic Lady era online. How long do sexual side effects of Lady era last? Where to buy Lady era no prescription. [url=https://clomiphenemenusa.yolasite.com/canadian-online-pharmacy.php]Canada Online Pharmacy[/url], Generic Lady era medication coupon.
Health & beauty indian Lady era. [url=http://aena.at/phpbb3/viewtopic.php?f=5&t=56109]Canadian Pharmacy[/url] Generic Lady era without a doctor prescription.

WilliamTap

03rd, Dec, 20

[url=http://prostitutki-don.com]девушку для секса без регистрации[/url], размещенные на нашем ресурсе, настолько прекрасны, что выбор будет сделать совсем не просто, но выбрав самую развратную красотку-путану, она воплотит в реальность самые смелые сексуальные фантазии.

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]