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

ASP to search within a range of dates

Home Forums Product Support Forums Ajax Search Pro for WordPress Support ASP to search within a range of dates

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #53280
    fabrisysfabrisys
    Participant

    Hello everyone,
    I am writing to you because I am trying to integrate Ajax Search Pro into a project developed on WordPress together with the Advanced Custom Field plugin.

    In this project I have to create a type of post that contains different types of data, but in particular in this type of post I have to use many fields (about 8) in the date format (Date Picker in the d/m/Y format). I have grouped all the dates in an ACF Repeater field (a Group field is probably also fine).

    I would like to use ASP to search within a range of dates. More precisely, I need to perform a search that allows me to find posts that contain dates within a range (for example: search for dates after 01/10/2020 and before 30/11/2020).

    I saw that ASP allows me to do this using “Custom Field” in “Frontend Search Settings”.

    I thought that I have to group the dates within a single Database field and I managed to do it by separating them with a comma.

    Unfortunately it only works partially. In fact I can do the search but it only works on the dates that are present as the first data in that field, while the dates after the first comma with are taken into consideration in the search.

    Hoping to have explained myself correctly, I hope to get some help from you to succeed in the intent.

    Below you can see the code (one of the many versions) that I used to group the fields in a single field of the database.

    function update_searchable_dates( $post_id ) {
    if ( have_rows(‘estremi_cronologici’, $post_id) ) {
    $index = 1; // Inizializza l’indice per distinguere i campi

    while ( have_rows(‘estremi_cronologici’, $post_id) ) {
    the_row();

    // Cicla su tutti i sottocampi (data_1, data_2, data_3)
    for ($i = 1; $i <= 3; $i++) {
    $date = get_sub_field(“data_$i”); // Leggi il sottocampo corrente

    if ($date) {
    // Salva ogni data in un campo separato con indice progressivo
    update_post_meta( $post_id, “_searchable_date_$index”, $date );
    error_log(“DEBUG: Salvato _searchable_date_$index -> $date”);
    $index++; // Incrementa solo quando trova una data
    }
    }
    }
    }
    }
    add_action(‘acf/save_post’, ‘update_searchable_dates’, 20);

    #53283
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    For this type of filter to work the dates must be in a completely separate field I’m afraid, otherwise it’s going to be extremely complicated. During the search the plugin compares the dates of the field. It is not possible to simply compare parts of the string as the comparision is done within the SQL query and it can’t split the field by commas, as it expect as date value, not a comma separated list of dates.

    I usually suggest a custom code snippet where I can, but this is much more complicated than that. The only possible solution I can see to this is by modifying the query arguments via asp_query_args to actually remove the date filter (or modify it so it does not apply during search). Then hook into asp_results to programmatically check the dates via a foreach loop and unset (remove) the unwanted results that does not fit the criteria.

    If you need professional help with that, I recommend our affiliates at wpkraken. I get asked for custom jobs a lot, but unfortunately I already have 10+ hours of work every day, 7 days a week, so I recommend them 🙂

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