Get WPFTS Pro today with 50% discount!

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



  • Conor Campbell asked:

    One of my clients utilises 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.


Log in to reply