Looper Type Custom Advice/Help?

Hi,

I’m trying to build a looper to retreive a custom post type (in this case Modern Events Calendar events mec-events) and have run into a problem getting my code to work.

I have created a WP_Query a follows (you’ll see what I’m trying to do from the code):

add_filter( 'cs_looper_custom_molly_events_data', 'molly_gigs_events_loop', 10, 1);
function molly_gigs_events_loop($result) {
	$today = date('Ymd');
	$args = array(
	    'post_type' 		=> 'mec-events',
	    'post_status'		=> 'publish',
	    'posts_per_page'	=> '9',
	    'meta_key'			=> 'mec_start_date',
	    'meta_query'		=>  array(
	            'key'			=> 'mec_start_date',
	            'value'			=> $today,
	            'compare'		=> '>='
		    ),
	    'orderby' => 'meta_value_num',
	    'order' => 'ASC'
	);
	$result = new WP_Query ($args);

	return $result;
}

I’ve then added a looper provider with a type of custom, and the hook pointed at molly_events_data:
image

However, I have now noticed that maybe the Custom looper type doesn’t accept a WP_Query like I have created? Your documentation here seems to suggest that my function should return some kind of JSON format?

Could someone please clarify what I am allowed to return from my ‘custom’ function, and the best way to acheive this? Otherwise, I’ll have to go back to coding this kind of thing manually…

Many thanks…

1 Like

OK, update:

It seems I can get the above to work (kind of) if I use a Looper Type of ‘Query String’ such as:

post_type=mec-events&post_status=publish&meta_key=mec_start_date&meta_compare=>=&meta_value={{dc:global:date format="Ymd"}}

I’ve tried adding the ‘global date’ thing above because the normal method of adding today’s date doesn’t seem to work? The above doesn’t work either… :frowning:

Sorry, my bad, I forgot to include the meta_type:

post_type=mec-events&post_status=publish&meta_key=mec_start_date&meta_type=DATE&meta_compare=>=&meta_value={{dc:global:date format="Ymd"}}

I’d still be grateful if someone could clarify my original question about the Looper Custom Type though…

Hi @sguilliard,

You can return any structure through the custom looper methods and print the respective attributes in the Looper Consumer elements.
But in this case, you need to return the posts object like in the following thread, so your return will $result->posts. I would suggest you go through the following thread on custom looper.



Hope it helps.
Thanks

Hi @tristup,

Thanks for this, very helpful! :slight_smile:

I’m assuming that getting the taxonomies for any post should be done with another query, or can I nest a looper and it be context sensitive? Just about to dive into the nested loopers section! :slight_smile:

Hi @sguilliard,

Glad that we are able to help you. To show the taxonomies for any posts you can use the dynamic content tag {{dc:term:name}} inside the main looper.

Hope it helps too.
Thanks

Hi @tristup,

I’m guessing this doesn’t work for custom post types? I ask because I thought I’d try {{dc:term:name}} inside the looper (I was expecting to have to build another looper to loop through the taxonomies & their terms) but it doesn’t seem to work?

HI @sguilliard,

It will work with custom post type, as long as your loopers call your custom post type, it will show the terms using that dynamic content {{dc:term:name}} .

Hope that helps.

Thank you.

Hi @marc_a,

The looper is calling the custom post type; I can get th CPT title and other fields, but no taxonomies or terms. I was expecting to have to run another looper inside the one that calls the CPT (this isn’t an archive page, just some dynamic content on a page being fed by the loopers).

Either way, it’s not working…

So the looper definitely works, in that anything ‘post’ related can be retrieved from the custom post type being pulled through from the looper. Out of the following DC strings, the ‘term’ ones don’t work, the ‘post’ one does:

{{dc:term:name}}
{{dc:term:description}}
{{dc:post:featured_image}}

Hello @sguilliard,

The {{dc:term:xxxxxx}} will only work for archive pages and layouts. If you are trying to displaying post or event information, you should be using {{dc:post:xxxxxxx}} dynamic content.

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.