• If I search and a post shows up in the first ten results, it always has an excerpt. If it shows up after the first ten results, no excerpt is displayed.

    Here is my query on the search page:

    global $query;
    $search_results = $query;
    
    $search_results->query_vars['s'] = get_query_var('s');
    $search_results->query_vars['posts_per_page'] = -1;
    
    relevanssi_do_query($search_results);

    then I’m looping through the posts:

    foreach($search_results->posts as $key => $result) {
    	$post = get_post($result->ID);
    	include('inc--search-preview.php');
    }

    In inc--search-preview.php I’m using get_the_excerpt() to get the excerpt. I have also tried relevanssi_the_excerpt() and the result was the same.

    • This topic was modified 8 years, 10 months ago by niemand_0. Reason: correcting code
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mikko Saari

    (@msaari)

    If you print out $search_results after the relevanssi_do_query(), what’s in it? All the posts found, I suppose, but what about the excerpts? Are there Relevanssi-generated excerpts for all posts, or just the first ten?

    Thread Starter niemand_0

    (@niemand_0)

    Oh, interesting!

    The Relevanssi-generated excerpts are there for all posts, in the post_excerpt property.

    It isn’t there, however, in my $post variable:

    foreach($search_results->posts as $key => $result) {
    	$post = get_post($result->ID);
    	include('inc--search-preview.php');
    }

    Is it because I’m not in the WordPress “loop”? I tried that but I get a “have_posts() is not defined” error:

    while($search_results->have_posts()) : $search_results->the_post();
    	include('inc--search-preview.php');
    endwhile;

    Am I missing something basic here? I haven’t built a query this way before so I’m not sure how to loop through it the “WordPress way”, rather than just doing a standard php foreach loop.

    Plugin Author Mikko Saari

    (@msaari)

    You’re using get_post() to fetch the post object. It loads the post object from the database, and that doesn’t include the Relevanssi excerpt.

    If you have Relevanssi returning post objects, you don’t need to do get_post(), $result is already a full post object. $post = $result;

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Excerpt only generated for first 10 posts’ is closed to new replies.