Skip Navigation

[Resolved] filter by date with custom shortcode not working

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use a custom shortcode to supply the value for my View's custom field filter but it doesn't seem to be filtering as expected.

Solution: Both the custom shortcode and the shortcode attribute must be added to the View shortcode in order for the Query Filter to respond to the dynamic value.

Relevant Documentation: https://toolset.com/documentation/user-guides/passing-arguments-to-views/

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.

This topic contains 4 replies, has 2 voices.

Last updated by MattI4840 1 year, 2 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#1276625

I am trying to:

I've created a view that displays a cpt called 'dances', there are two filters applied, one for the 'entertainer' who performed the dance (that filter works fine), the other is a date time filter that uses a custom shortcode ([dailydances]) to capture the epoch time of the previous day at 1pm, and compare it to the date captured in a date-time field for the 'dance' cpt. Any dances that took place after the previous day at 1pm should populate the view, but the comparison does not appear to be working correctly as I always get all entries returned.


add_shortcode( 'dailydances', 'yls_daily_dances' );

function yls_daily_dances() {

	$today              = strtotime('13:00:00');
	$yesterday          = strtotime('-1 day', $today);
	return $yesterday;

}

Link to a page where the issue can be seen:

It's in my local dev environment, but the screenshots should provide what you need. Note in the filter-example I've applied the filter, and as you'll see I printed the shortcode for easy comparison. The last two entries should not be visible as they took place before 'yesterday at 1pm'.

#1276669

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hi, have you registered the custom shortcode in Toolset > Settings > Frontend Content > Third Party Shortcode Arguments? That's the first thing you should confirm. Then if that's not the problem, please copy + paste the exact shortcode combination you are using to insert the View. I'll take a quick look and see if anything looks out of place.

#1276675

Yes I've registered the shortcode, please see the attached screen. I'm only using the shortcode in the filter for the view as the previous screenshot 'view-filters' shows. The below is what is used to insert the view on the page.

<div class="wp-block-toolset-view">[wpv-view name="daily-dance-report"]</div>
#1276679

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay I see the confusion here. When you type "dailydances" as the Shortcode value in the Query Filter, this is the name of the shortcode attribute that will be used to supply the dynamic filter value. It has nothing to do with the custom shortcode you created per se, except that you will combine the two in the View shortcode to pass the dynamic value into the filter. You must update your View shortcode as shown here:

<div class="wp-block-toolset-view">[wpv-view name="daily-dance-report" dailydances="[dailydances]"]</div>

There is a shortcode attribute "dailydances" because that's what you chose to call it in the Query Filter. The nested dailydances shortcode is the custom shortcode you created to supply the filter a dynamic value. Both are required to make the Query Filter respond to your custom shortcode. It is not necessary to name them identically. Make sense?

#1276683

My issue is resolved now. Thank you!