• Resolved klewis

    (@blackawxs)


    Either with or without ACF Pro fields, what steps can I take with this plugin to search any post by modified date?

    I made an attempt of trying ACF Pro’s acf/load action by using the following code in my functions.php file to pre-fill the modified date into a ACF field for an associated Post. But no luck on searching for it yet…

    
    function my_acf_load_dte_modifield_value( $value, $post_id, $field ) {
      $date_modifield=get_the_modified_date( '', $post_id );
      return $date_modifield;
    }
    add_filter('acf/load_value/name=mpreloadedvalue', 'my_acf_load_dte_modifield_value', 10, 3);
    
    
    • This topic was modified 3 years ago by klewis.
    • This topic was modified 3 years ago by klewis.
    • This topic was modified 3 years ago by klewis.
    • This topic was modified 3 years ago by klewis.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mikko Saari

    (@msaari)

    This problem has two parts: one is getting Relevanssi to index the modified date and one is to make Relevanssi search dates in the first place. The first one is easy:

    add_filter( 'relevanssi_content_to_index', function( $content, $_post ) {
      return $content . ' ' . get_the_modified_date( '', $_post->ID );
    }, 10, 2 );

    This will add the modified date to the post content.

    Dates, however, are generally not very friendly for searching. Which date format are you using? All date formats are basically non-searchable. With the default settings, “Jan/1/2022” is indexed as “Jan 2022” and “1.1.2022” or “1/1/2022” is indexed as “2022”. The best format is “January 1st 2022”, which is indexed as-is, but even that is not very practical for searching.

    The problem is that Relevanssi replaces the punctuation with spaces and doesn’t index anything shorter than three characters. So, you need to modify the Relevanssi punctuation control so that dates are kept intact. The exact method depends on the date format you’re using. If you’re using “1/1/2022”, for example, you need to make Relevanssi keep the slashes so that the date is indexed as “1/1/2022”.

    add_filter( 'relevanssi_remove_punctuation', function( $a ) {
        $a = preg_replace( '/(\d+)\.(\d+)\.(\d+)/', '\1DATEPERIOD\2DATEPERIOD\3', $a );
        return $a;
    }, 8 );
    
    add_filter( 'relevanssi_remove_punctuation', function( $a ) {
        $a = str_replace( 'DATEPERIOD', '.', $a );
        return $a;
    }, 11 );
    Thread Starter klewis

    (@blackawxs)

    Mikko Saari, many thanks on showing the complexities that we will face in trying to achieve this objective. I can now see this is not a “natural” integration process.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to search posts by modified date’ is closed to new replies.