This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Show/hide results based on a datetime metafield

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Show/hide results based on a datetime metafield

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #52960
    samuelepellizzarisamuelepellizzari
    Participant

    Hi Ernest,

    I was wondering if there were a way to show/hide results based on a metafield indicating the date.

    More specifically, I set up my ‘events’ CPT with the ‘start-date’ metafield: this is coded in standard UNIX timestamp format. I would like to show only the results whose ‘start-date’ is greater or equal to today.

    Is that possible?

    Many thanks,
    Samuele

    #52968
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi Samuele,

    Yes – it is possible two ways – one is a bit of custom code snippet, or if you are not using the search filters, then that is also an option. Search filters are effective even if they are invisible.

    Therefore you can create a date filter on that field like so: https://i.imgur.com/rGCSVpw.png
    This will filter out results where the “start-date” is before today.

    #52972
    samuelepellizzarisamuelepellizzari
    Participant

    Hi Ernest,

    Many thanks for your reply.

    Your link seems broken.

    Anyway, I would rather go for the code snippet way, since I’m not using filters for the time being.

    Have you got anything ready that I can tweak?

    Thanks,

    Samuele

    #52976
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Sure Samuele! I would also prefer a custom code snippet if I were you, good choice 🙂

    This is what you are looking for:

    add_filter("asp_query_args", "asp_query_args_apply_date", 10, 2);
    function asp_query_args_apply_date($args, $search_id) {
    	$args['post_meta_filter'][] = array(
    		'key'     => 'start-date',
    		'value'   => strtotime("today", time()),
    		'operator' => '>=',
    		'allow_missing' => false
    	);
    	return $args;
    }

    Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    #52990
    samuelepellizzarisamuelepellizzari
    Participant

    Hi Ernest,

    Many thanks indeed for sharing the snippet.

    I forgot to to add a detail: the plugin is set up to search through 3 different CPTs (events, products and people, i.e. the authors of our books) and works pretty well by also grouping the results. However, with your snippet activated, I get ‘events’ results only, the ones that feature such ‘start-date’ metafield.

    Shall I edit the last part as follows?
    'allow_missing' => true

    Would that be enough or do I need to add anything else?

    Thanks again,

    Samuele

    #52992
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi Samuele,

    'allow_missing' => true should do the trick. It will allow results where the custom field does not exist and apply the filter where it does.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.