WordPress.org

Support

Support » WP-Advanced » [Resolved] the_excerpt() ignores explicit excerpts from custom query

[Resolved] the_excerpt() ignores explicit excerpts from custom query

  • I’m using a custom query to extract and display my explicit excerpts of child pages using the_excerpt() outside the loop.

    Everything works fine except that WordPress displays an auto-generated excerpt rather than the one stored in the database.

    The query is selecting everything:
    `$wpdb->get_results(“
    SELECT *
    FROM $wpdb->posts`
    so I would assume that the post_excerpt is included in there.

    And is followed by a foreach loop using setup_postdata().

    If I replace the_excerpt() with get_the_excerpt() I get a big blank.

    It looks to me like the database post_excerpt is getting lost somewhere. Is this a bug or do I need to do something else to get my excerpts back?

Viewing 8 replies - 1 through 8 (of 8 total)
  • What are you doing with get_the_excerpt()? It returns (and does not echo) it’s value, so if you are simply replacing the_excerpt(), it will appear to be doing nothing.

    Doh!

    OK <cough> – echo out get_the_excerpt() and again I get the auto-generated excerpt instead of my explicit saved excerpt.

    To be clear, I can see my excerpt if it’s called using the usual methods inside the loop, so I know it exists and that WordPress can display it.

    My guess would be that either $wpdb->get_results() is losing post_excerpt or that setup_postdata() is ignoring it. The latter seems more likely, but I can’t find any documentation on setup_postdata() and I don’t know enough about WordPress to make sense of it in query.php (line 1494)

    I’ll take a closer look at this to see if there’s bug here (and needs to be reported), but for now you could use this to display your ‘explicit’ excerpt:

    <?php echo apply_filters('the_excerpt', $post->post_excerpt); ?>

    -or-

    <?php echo apply_filters('the_content', $post->post_excerpt); ?>

    The latter providing all the fancy text formatting the post content normally takes on.

    Thanks for this.

    Curiouser and curiouser… Now I do get exactly nothing – well I get a <br /> in the space where the excerpt is supposed to go. In the second version of the function above I get an indented <br />.

    What var is holding your post data? You may need to change $post in my code above.

    We have a result!

    Thanks. So, I now have a working solution, but it’s a workaround and it does mean that something funky is happening inside setup_postdata().

    Do I mark this thread ‘resolved’ or should I leave it open for a day or two?

    Well, *your* issue is resolved, so you can go ahead and flag it as such. But I’ll be following up on the problem you had with (possibly) setup_postdata at WordPress’ bug report site, which I’ll then link to here.

    I didn’t see this thread and responded to an older thread with similar problem. The code I provided used a variable named $pageChild to hold data during a loop which iterates over the output of $wpdb->get_results(). If the $pageChild variable is renamed as $post, the errors for the_excerpt seem to disappear. Hope this helps in your bug search,

    -mfields

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] the_excerpt() ignores explicit excerpts from custom query’ is closed to new replies.