WordPress.org

Ready to get started?Download WordPress

Forums

How can i display a random excerpt from custom posts? (9 posts)

  1. desi4eva
    Member
    Posted 1 year ago #

    Can someone help please?

    I am trying to allow my blog which is using custom posts to show random excerpts when a search is done. it doesn't have to be ordered just random. As many of the posts start with the same sentences the excerpts all look the same so i would rather have it randomized.

    Any help with this is much appreciated thanks!

  2. Spencer Cameron
    Member
    Posted 1 year ago #

    Use this to query for posts in random order:

    $query = new WP_Query( array( 'post_type' => 'use_your_custom_post_type', 'orderby' => 'rand', 'no_found_rows' => true ) );

    Your loop ( on the search page, I assume. ) could look something like this now:

    <?php $query = new WP_Query( array(
                                        'post_type' => 'use_your_custom_post_type',
                                        'orderby' => 'rand',
                                        'no_found_rows' => true ) ); ?>
    
    <?php while( $query->have_posts() ) : $query->the_post(); ?>
        <?php // do your loop stuff here ?>
    <?php endwhile; ?>

    I haven't tested this, but it should do the trick. Let me know if you have a problem. :)

  3. desi4eva
    Member
    Posted 1 year ago #

    Sorry this is not to have the posts in random order but to show random selection of the excerpt instead. So say the posts are in order but the excerpt being shown is randomly selected from the posts content.

  4. Spencer Cameron
    Member
    Posted 1 year ago #

    Ouch. You'd need to parse the excerpt content in a custom function and then return a random portion. I'll see if I can mock something up.

  5. desi4eva
    Member
    Posted 1 year ago #

    Thank you so much!

    [ Please do not bump, that's not permitted here. ]

  6. Spencer Cameron
    Member
    Posted 1 year ago #

    Hi desi4eva,

    I completely forgot about this. Do you still need help on it or have you solved it?

  7. desi4eva
    Member
    Posted 1 year ago #

    Yes please sorry if you can.

  8. Spencer Cameron
    Member
    Posted 1 year ago #

    I'll make something for you this evening :)

  9. desi4eva
    Member
    Posted 1 year ago #

    Ok thank you :)

Topic Closed

This topic has been closed to new replies.

About this Topic