Skip Navigation

[Resolved] Filter by custom date field to find posts from within the current month

This support ticket is created 8 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 8.91 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 -
- - - - - - -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

Tagged: 

This topic contains 31 replies, has 3 voices.

Last updated by Adriano 8 years, 1 month ago.

Assisted by: Adriano.

Author
Posts
#368390

There was some mistake in the code indeed, fixed now and I think it's more clear now:

[php]
add_filter('wpv_filter_query', 'posts_within_current_month', 10, 2);
function posts_within_current_month($query, $settings) {
if($settings['view_id'] = 123)
{
$query['meta_query'] = array(
array(
'key' => 'start_date',
'value' => array( strtotime('+1 month', date('Y-m-01')), strtotime('+1 month', date('Y-m-01'))),
'type' => 'date',
'compare' => 'BETWEEN',
),
),
}
return $query;
}
[php]

#368428

Hi Adriano

Thanks, but I just want to check, if I want the posts from the 1st to the 31st, should the second

strtotime('+1 month', date('Y-m-01'))

not actually be ('Y-m-31')??

Or am I misunderstanding what these are?

#368507

Oh man, my mistake again 🙁

add_filter('wpv_filter_query', 'posts_within_current_month', 10, 2);
function posts_within_current_month($query, $settings) {
	if($settings['view_id'] = 123)
	{
		$query['meta_query'] = array(
			array(
			'key' => 'start_date',
			'value' => array( strtotime('+1 month', date('Y-m-01')), strtotime('+1 month', date('Y-m-t'))),
			'type' => 'date',
			'compare' => 'BETWEEN',
			)
		);
	}
	return $query;
}

You should use t, because t represents the number of days in the month.

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,

Adriano Ferreira

#369180

Hi Adriano

I've tried to add this function, but when I do I get totals of 0 for every month??

I replaced the view id and the key (should this be in the format wpcf-start-date? I also tried start-date, but this didn't work either.)

I don't need to call the function from the loop output hey? It should just filter it from the view id huh?

Can you send me private message link so you can go in and check?

#369212

Private details asked.

#369339

I can't access the site, is it online?

#369490
IMA Login.png

Hi Adriano

I can get to it on my side. Do you not get to the attached login screen?

#369654

No, I can't access. Is there any kind of protection there, maybe by IP?

#369706

Hi Adriano

I have disabled a couple of plugins that might be blocking you.

If you still cant get in please send me a screenshot or describe what it says when you try and go there.

#369709

Nothing, it's loading forever. No message displayed.

#370050

Hi Adriano

I asked my host and they say there might be something on their side blocking you.

Which country are you trying to access from?

Thanks

Gavin

#370140

Brazil.

#370864

Hi Adriano

IP blocking should be cleared now?

#370984

Great! What about FTP? I need that to send some changes.

#371561

I get the final code:

add_filter('wpv_filter_query', 'posts_within_next_month', 10, 2);
function posts_within_next_month($query, $settings) {
    if($settings['view_id'] = 1528)
    {
        $date_from = new DateTime( date( 'Y-m-01' ) );
        $date_to = new DateTime( date( 'Y-m-t' ) );
        $interval = new DateInterval( 'P1M' );

        $date_from->add($interval);
        $date_to->add($interval);



        $query['meta_query'] = array(
            array(
            'key' => 'wpcf-start-date',
            'value' => array( strtotime($date_from->format('Y-m-d')), strtotime($date_to->format('Y-m-d'))),
            'compare' => 'BETWEEN',
            )
        );
    }
    return $query;
}

Is this fine now?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.