Saltar navegación

[Resuelto] View Search using a custom shortcode

This support ticket is created hace 4 años, 12 meses. 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)

Etiquetado: ,

Este tema contiene 3 respuestas, tiene 2 mensajes.

Última actualización por Luo Yang hace 4 años, 11 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#1529837

I have a Toolsets field with a year in it (the year that someone got a qualification).

I use this to create a shortcode that displays the number of years experience from today - I've registered this and its working.
https://toolset.com/forums/topic/calculating-dates/ (slightly modified as I only want the year).

What Id like to do is create a front end filter where I can specify a range - e.g this person has over 10-years experience or between say 5 - 7 years experience? Is this possible at all?

I cant see how, but thought I'd ask 🙂

#1530171

Hello,

How do you setup the custom field "the year that someone got a qualification"?
I assume it is custom date field, there isn't the exact feature within Toolset, there is a date range filter, see our document:
https://toolset.com/documentation/user-guides/views/date-filters/#filtering-between-dates

In your case, you might consider custom codes, for example:
- Use filter hook "wpv_filter_query" to trigger a PHP function
- In this PHP function, get the user's submitted inputs, and turn them into timestamp values, and apply the values to filter the queue
more help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.

#1537133

The field is actually not a date field, its a number field.

I'm struggling how to understand how I achieve this with filter hook above... are there any examples of how

I use this code to produce a the number of years since today from the number field that contains the year.

e.g:
Years Experience: 56 <-- short-code result

From: Admission Year Numeric: 1964 <-- custom field

<?php
/**
 * https://toolset.com/forums/topic/calculating-dates/
 */
toolset_snippet_security_check() or die( 'Direct access is not allowed' );

/**
 *  Calculate age (in years) from date field
 *  specified as shortcode attribute 'slug'
 */
add_shortcode( 'age', function( $atts = [], $content = null ){
 
    // provide defaults
    $atts = shortcode_atts( 
        array(
            'slug'      =>   null
        ), 
        $atts
    );
 
    $age = '';
 
    if ( isset( $atts['slug'] ) ) {
 
        global $post;
        $timestamp = get_post_meta( $post->ID, 'wpcf-' . $atts['slug'], true );
 		
		$dateTime = DateTime::createFromFormat("Y", $timestamp);		
 
        $now = new DateTime();
 
        $diff = $now->diff( $dateTime );
 
        $age = $diff->y;
    }
 
    return $age;
});

I then want to some how have a drop down menu filter to show, based on that short-code value (as it changes relative to the current year).
> 1 year Experience
> 2 year Experience
> 3 year Experience
> 4 year Experience
> 6 year Experience
> 7 year Experience
> 8 year Experience
> 9 year Experience
> 10 year Experience
> 11 year Experience
> 12 year Experience
> 13 year Experience
> 14 year Experience
> 15 year Experience
> 16 year Experience
> 17 year Experience
> 18 year Experience
> 19 year Experience
> 20 year+ Experience

I just cant understand how i create a drop down filter to work like this... any examples would be helpful, Ive read the doco on wpv_filter_query but I am more confused.

thanks

#1538331

I assume the custom field "the year that someone got a qualification" stores value as below:
- 2000
- 2010
...

If it is, there isn't such kind of built-in feature within Toolset plugins, as I mentioned above it needs custom codes, but according to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/

Q1) are there any examples of how
There an example in the document I mentioned above:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

But it shows only for how to use filter hook wpv_filter_query, there isn't exact example for your case.

Q2) I just cant understand how i create a drop down filter to work like this
You can setup the drop down menu with HTML codes manually, for example:
enlace oculto

You might check it with our Toolset Contractors:
https://toolset.com/contractors/