Skip Navigation

[Resolved] Create custom calculated filter

This support ticket is created 7 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 3 voices.

Last updated by Luo Yang 7 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#556354

Hello,

I have a view showing posts with parametric search and custom filters. On of the custom filter is created with

add_filter( 'wpv_filter_query',

On the other hand, I have a shortcode that calculates if a post is older than a custom date field or not, with

add_shortcode( 'validite',

Now I would like to create a custom parametric search filter that would allow the final user to choose to show only older or newer children posts.

What would be the best approach to do this ?

Thank you very much.

#556475

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - you can use [wpv-conditional] statement inside your view's loop output editor to check if your post is old or new.

You can use your custom shortcode inside [wpv-conditional] shortcode to check if your post is old or new.

More info:
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/
[Section: "Checking Custom shortcodes"]

#556493

Thank you, but I'm not sure I understand how the conditionnal+shortcode makes a parametric search filter field ?
Thnak you.

#556610

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No - you can not create a parametric search filter that way.

But if you share exact information, based on which field you are deciding that post is old or new. I believe if its custom date field then view's filter wpv_filter_query might be helpful here but I need problem URL and temporary access.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#557154

Dear Roman,

I checked the source code of your theme file functions.php, the source code of shortcode [validite], PHP codes:

/**************************************************
 * Shortcode pour déterminer si une offre est valide maintenant
 **************************************************/
function determiner_validite_offre( ) {
	$post_id = get_the_ID();
	
	$date_emission = get_post_meta($post_id,'wpcf-date-d-emission-de-l-offre', true);
	$duree_validite = get_post_meta($post_id,'wpcf-duree-de-validite', true);
	$date_expiration = get_post_meta($post_id,'wpcf-date-d-expiration-de-l-offre', true);
	$maintenant = current_time( 'timestamp' );

	if ($date_expiration < ($maintenant)) {return "Expirée";}	else {return "Valide";}
}
add_shortcode( 'validite', 'determiner_validite_offre' );

It is checking custom field "wpcf-date-d-expiration-de-l-offre" value, if it is bigger than current time, then display "Valide", else display "Expirée"

I assume you are going to select menu with three options:
"all offers", "only valid offers", "only expired offers"

Since the custom field "wpcf-date-d-expiration-de-l-offre" stores value as time stamp value, which is numeric value, so there isn't such a built-in feature within Views plugin to display as what you want.

In your case, it needs custom PHP codes, you can apply your custom filters with Views filter hook "wpv_filter_query", But since it is not built-in feature of Views plugin, the option " Show only available options for each input" will not work with the custom PHP codes, please let me know if you still need assistance to apply such kind of custom PHP codes.

#557170

In deed I really don't understand how to build the PHP code to make my filter work.

If I understand well, to make the filter work in parametric search, I would need to also add a generic field, that would be populated by the PHP filter ?

#557202

Yes, you are right, for example:
1) In the custom search form, you will need to add a dropdown menu with options:
"all offers", "only valid offers", "only expired offers"
User can use it to choose one of option, after submit the form, it will pass specific URL parameter to the view

2) In server side, you can setup custom PHP codes with Views filter hook "wpv_filter_query", when user submit the custom search form, it will trigger a custom PHP function, in this PHP function, check the pecific URL parameter, if it is option "only valid offers", then filter the view by this:
Custom field "wpcf-date-d-expiration-de-l-offre" value is greater than current time value.

Hope it is clear.

#558845

Please still don't close this thread.
Thank you.

#558891

Please update this thread when you need more assistance, thanks

#559860

Please don't close this thread now ; I need some time to check on your solution.
Thank you.

#559870

It is the forum system who are going to close this thread. I mark it as status "Waiting for user feedback", please update this thread when you need more assistance, thanks