Custom Looper provider with WP_Query - Not working?

Hey there, I hope you’re having a great day :slight_smile:

I know what I’m about to ask is custom coding, but I’ve exhausted my options and wondered whether something may be broken with the custom looper provider… So here I am!

I’ve got a custom post type called “Social Events” which has some custom fields. I want to order the posts by the event start date meta setup by ACF as well as only show future events, hence the custom provider hook requirement.

I’ve got a div set as looper provider set to custom with a hook of get_recent_social. It’s also set as the consumer with some elements inside to output post title etc.

In the backend I’ve got the below filter:

    function ltm_get_recent_social($result){
	$date_now = date('Y-m-d H:i:s');
	$args = array(
		'post_type'             => 'social',
		'post_status'           => 'publish',
		'meta_key' 				=> 'start',
		'orderby' 				=> 'meta_value',
		'order' 				=> 'ASC',
		'meta_query'     => array(
        			array(
            			'key'         => 'start',
            			'compare'     => '>=',
            			'value'       => $date_now,
            			'type'        => 'DATETIME'
        			)
    	),
	);
	$query = new WP_Query( $args );
	return $query->posts;
}
add_filter( 'cs_looper_get_recent_social', 'ltm_get_recent_social', 10, 1);

The issue is that the looper says there aren’t any posts coming through. Now, if I var_dump($query->posts) it spits out an array of 3 WP_Post objects, which is accurate.

So my conclusion is that either I’m not returning things in the correct format OR something is wrong with the custom looper system. Can you please help?

1 Like

Hi @conorseed,

I think you’re close! I’ve not tested this out and I’m not sure if this will solve it. For cases when you simply need post data and don’t need to worry about managing a WordPress loop, it’s usually better to use the get_posts function instead of WP_Query. So something like this:

return get_posts( $args );

Either try that, or perhaps this:

$query = new WP_Query;
return $query->query( $args );

Hopefully that helps get things working!

Thanks @alexander. Appreciate the response, and the formatting tips :wink:

Bad news; unfortunately neither of those options worked for me… On closer inspection it seemed like the filter wasn’t firing at all - I added some error logging in there which didn’t get triggered. So I hooked the function into a shortcode, and it fired perfectly there.

Good news: Turns out my filter was wrong :man_facepalming: It should be cs_looper_custom_$hook and I had cs_looper_$hook. What an idiot lol.

Thanks for your time anyway!

You are most welcome.

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