Get WPFTS Pro today with 25% discount!

[Solved] Show Smart Excerpt for attachments with WP_Query()?


  • Sometimes it can be a problem to show the content of the attachment file in search results using the Smart Excerpt way.

    Conor Campbell asked:

    One of my clients utilizes your WordPress plugin to index documents on their site.

    It works great, however, I am unable to get excerpts to appear for
    PDFs, as they're using a custom search results page. What
    information do you require from me in order to investigate this?

    Looking forward to your response.

    Yes, the Smart Excerpt function is only activated automatically when you
    are making WP Main Search. In case you want to force Smart Excerpts for
    custom WP_Query() request, you need to do this:

    global $wpfts_core;
    
    $result = new WP_Query(array(
         's' => $string_query,  // A search string which you're searching for
        ...other your parameters to filter results...
    ));
    
    // This will enable Smart Excerpts
    $wpfts_core->ForceSmartExcerpts($string_query);
    
    // Here is your code to loop over $result to view search results
    // You need to use the_excerpt(); to show Smart Excerpts
    
    // Check that we have query results.
    if ( $query->have_posts() ) {
    
        // Start looping over the query results.
        while ( $query->have_posts() ) {
     
            $query->the_post();
     
            // Contents of the queried post results go here.
            // the_title();
            // the_excerpt();
            //....
        }
     
    }
     
    // Restore original post data.
    wp_reset_postdata();
    
    // You may disable Smart Excerpts if you need to (OPTIONAL)
    $wpfts_core->ForceSmartExcerpts(false);
    

    Hope this helps.


  • @EpsilonAdmin Thank you, that does help.

    Is it possible to display the PDF content as the excerpt of a post it is embedded in though?

    For example: "Sample Post" has no content except for an embedded PDF. I would like some text from that PDF to display in the search results, but the permalink goes to "Sample Post".


  • Yes, you can do that. But it will need some coding.

    First of all, we need to extract a PDF link or file path from the post. It should be done at indexing stage, inside the 'wpfts_index_post' hook function.

    After the link/path extraction, we should read this file and extract the text information from it. It still should be done inside the same hook.

    Okay, when we have the text, we put it to one of the index clusters (your preference) and let it to the indexer to make its job as well.

    The final code will depend from the way how you have attached the PDF file to the parent post. It can be done using very different ways.

    Please give me some more explanation of this and I will post the semi-ready code here.

Suggested Topics

Be the first to read the news!

We are always improving our products, adding new functions and fixes. Subscribe now to be the first to get the updates and stay informed about our sales! We are not spammy. Seriously.

Join Us Now!

We are a professional IT-team. Many of us have been working in a Web IT field for more than 10 years. Our advanced experience of software development has been employed in the creation of the WordPress FullText Search plugin. All solutions implemented into the plugin have been used for 5 or more years in over 60 different web-projects.

We are looking forward to your comments, requests and suggestions in relation to the current plugin and future updates.

ewm-logo-450

The forum powered by NodeBB | Contributors