• Alberto

    (@albert0deavila)


    Good morning,

    Relevanssi can index attachment contents from files linked to posts with ACF File fields. This does not happen automatically but requires some extra code.

    For “File Array”:

    add_filter( 'relevanssi_content_to_index', 'rlv_get_acf_file', 10, 2 );
    add_filter( 'relevanssi_excerpt_content', 'rlv_get_acf_file', 10, 2 );
    
    function rlv_get_acf_file( $content, $_post ) {
        $file = get_field( 'pdf_file', $_post->ID );
        if ( is_array( $file ) && isset( $file['ID'] ) ) {
            $content .= ' ' . get_post_meta( $file['ID'], '_relevanssi_pdf_content', true );
        }
        return $content;
    }

    I add the code to my site and rebuild the index, but searching by pdf file name does not show any posts as a result.

    I have Relevanssi free installed.

    In this line I substituted ‘pdf_file’ by field name:

    • get_field( ‘pdf_file’, $_post->ID );

    https://postimg.cc/bS9BLhJN

    Can you help me?
    Thank you so much

    • This topic was modified 1 year, 10 months ago by Alberto.
    • This topic was modified 1 year, 10 months ago by Alberto.
    • This topic was modified 1 year, 10 months ago by Alberto.
    • This topic was modified 1 year, 10 months ago by Alberto.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mikko Saari

    (@msaari)

    That function does not index the PDF file name. It indexes the PDF contents, if you have Relevanssi Premium.

    Instead of

    $content .= ' ' . get_post_meta( $file['ID'], '_relevanssi_pdf_content', true );

    you can try

    $content .= ' ' . get_the_title( $file['ID'] );

    This should fetch the attachment post title and include that in the post.

    Thread Starter Alberto

    (@albert0deavila)

    But I want it to search by file name: file.pdf

    Plugin Author Mikko Saari

    (@msaari)

    Then replace that line with something that fetches the file name based on the file ID. I don’t know what that is, offhand, but I’m sure ACF support can help you out with that.

    Thread Starter Alberto

    (@albert0deavila)

    I don’t have support because I have the free plugin

    Plugin Author Mikko Saari

    (@msaari)

    Try the ACF forums. I’m sure someone there knows.

    Thread Starter Alberto

    (@albert0deavila)

    This is the solution:

    add_filter( 'relevanssi_content_to_index', 'rlv_get_acf_file', 10, 2 );
    add_filter( 'relevanssi_excerpt_content', 'rlv_get_acf_file', 10, 2 );
    
    function rlv_get_acf_file( $content, $_post ) {
        $file = get_field( 'pdf_file', $_post->ID );
        if ( is_array( $file ) && isset( $file['ID'] ) ) {
            $content .= ' ' . $file["filename"];
        }
        return $content;
    }

    Do you know how to make relevanssi also look for the filename of the links in the page content?

    • This reply was modified 1 year, 10 months ago by Alberto.
    • This reply was modified 1 year, 10 months ago by Alberto.
    • This reply was modified 1 year, 10 months ago by Alberto.
    Plugin Author Mikko Saari

    (@msaari)

    Here’s how you could approach it:

    add_filter( 'relevanssi_content_to_index', function( $content ) {
      preg_match_all('/<a\s[^>]*href\s*=\s*[\'"]([^\'"]+)[\'"][^>]*>/', $content, $matches);
      $href_attributes = $matches[1];
      foreach ($href_attributes as $href) {
        if ( str_ends_with( $href, '.pdf' ) ) {
          $content .= ' ' . basename( parse_url( $href, PHP_URL_PATH ) );
        }
      }
      return $content;
    } );

    This would index the file names from all the links to PDF files from the post content. I haven’t tested this code, but it’s mostly from ChatGPT which is pretty good with stuff like this.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘ACF: Indexing files from File fields’ is closed to new replies.