1

I'm trying to build a structure like this (see most excellent diagram) with two different CPTs, Videos and Ads.

I want to blend the two post types in the same wp-query so they are output in a specific pattern: 3 videos, one ad, 3 videos, one ad -- up to a maximum of 16 posts. I don't know if this is even possible, or requires magic of some sort to make it work.

I have to consider that on a mobile view, there will be a single column of posts, and this same pattern must be repeated i.e. 3 videos then an ad, repeat. Therefore using two WP Queries in a 2 column layout with videos on the left (3 per row) and ads on the the right (displaying a single ad per row), is going to end up with it displaying a bunch of videos (left column first), and then a bunch of ads at the end (i.e. the right column). Not optimal. They must be mixed, somehow.

enter image description here For what it's worth, I'm using JetEngine listing grid to display these posts, which has quite comprehensive WP-query settings. I have not found a way yet to specify a WP Query object. In any case, I am very happy to know if this is possible to do with WP-Query, whether or not it would work with JetEngine.

New contributor
turpachull is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • You will not find a combination of WP query parameters that will generate a result like this with a single query. If you query both posts types, your results will be two arrays of posts and you can write your own PHP loop and iterate that to create your layout. – jdm2112 Mar 25 at 17:14
  • Thanks, I knew it had to be something like this. Confirms my suspicions. :) – turpachull Mar 26 at 10:46
0

Since jet engine works on Elementor, you can add a custom query filter

This may sound a bit complex, but it can work for your case.

  1. Add a hook that on save post will create a array of articles and ads in the order you want. Store the post Ids in a transient.

  2. In the Elementor query filter, get the post id's from the transient, and rested the query args to be post__in with the post ids from the transients.

  3. In the jet engine widget, set the filter ID and you will get the posts you set.

The reason transient is better than query inside the filter, it will avoid you gettings loops of error maximum depth when you set a query inside a query. Also it will be faster.

Elementor custom query filter

https://developers.elementor.com/custom-query-filter/

Save post hook

https://developer.wordpress.org/reference/hooks/save_post_post-post_type/

Transients

https://developer.wordpress.org/apis/handbook/transients/

  • Thanks for the very helpful answer! It appears, unfortunately, that the listing grid widget has no setting for specifying the query filter. – turpachull Mar 26 at 10:43
  • Can you add screenshot of that widget settings? – BenB Mar 26 at 21:38

Your Answer

turpachull is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.