Support » Developing with WordPress » Random Post, Max Character Length

  • Resolved jabbamonkey

    (@jabbamonkey)


    I’m trying to show a random posts on a page, but want to only show a random post that is under 180 characters. So far, I have the query below… can someone help with the character limit?

    <?php
    $args = array('numberposts' => 1, 'orderby' => 'rand');
    query_posts($args);
    
    while (have_posts()) : the_post();
      the_content('Read the full post »');
      // And so forth…
    endwhile;
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Alex Mills

    (@viper007bond)

    You should use WP_Query instead of query_posts() to avoid overwriting global variables and messing up conditional tags.

    https://developer.wordpress.org/reference/classes/wp_query/

    To only get posts that are under 180 characters, you’d need to modify the SQL query directly using a filter. This completely untested code should give you something to go on:

    
    add_filter( 'posts_where', 'jabbamonkey_only_fetch_short_posts', 10, 2 );
    $your_query = new WP_Query( $args );
    remove_filter( 'posts_where', 'jabbamonkey_only_fetch_short_posts', 10 );
    
    function jabbamonkey_only_fetch_short_posts( $where, $query ) {
    	$where .= ' AND CHAR_LENGTH(post_content) <= 180';
    
    	return $where;
    }
    
    jabbamonkey

    (@jabbamonkey)

    I threw the code into my homepage, and it spits out the homepage content (not a random post)

    <?php
    $args = array('numberposts' => 1, 'orderby' => 'rand');
    add_filter( 'posts_where', 'only_fetch_short_posts', 10, 2 );
    $your_query = new WP_Query( $args );
    remove_filter( 'posts_where', 'only_fetch_short_posts', 10 );
    
    function only_fetch_short_posts( $where, $query ) {
    	$where .= ' AND CHAR_LENGTH(post_content) <= 180';
    	return $where;
    }
    
    while (have_posts()) : the_post(); ?>
      <?php the_content(); ?>
    <?php endwhile; ?>
    Moderator bcworkz

    (@bcworkz)

    Try this version. Be sure the posts table prefix in the CHAR_LENGTH() function is correct for your site.

    <?php
    $args = array('posts_per_page' => 1, 'orderby' => 'rand');
    add_filter( 'posts_where', 'only_fetch_short_posts', 10, 2 );
    $your_query = new WP_Query( $args );
    remove_filter( 'posts_where', 'only_fetch_short_posts', 10 );
    
    function only_fetch_short_posts( $where, $query ) {
    	$where .= ' AND CHAR_LENGTH(wp_posts.post_content) <= 180';
    	return $where;
    }
    
    while ( $your_query->have_posts()) : $your_query->the_post(); ?>
      <?php the_content(); ?>
    <?php endwhile; ?>

    The loop you were running was for the main query, not your custom query.

    jabbamonkey

    (@jabbamonkey)

    It’s coming together… but right now it’s showing 10 random posts…

    If this part correct?
    ‘numberposts’ => 1

    Moderator bcworkz

    (@bcworkz)

    AFAIK numberposts is deprecated but should have still worked. It has apparently been fully removed now, because it doesn’t work for me either. It was replaced with “posts_per_page”. Use that instead (like in my example :P) and you’ll only get one random post returned meeting the WHERE criteria.

    jabbamonkey

    (@jabbamonkey)

    Got it. It’s all working now. Thanks for all the help everyone! Final code is below…

    <?php
    $args = array('posts_per_page' => 1, 'orderby' => 'rand');
    add_filter( 'posts_where', 'only_fetch_short_posts', 10, 2 );
    $feature_query = new WP_Query( $args );
    remove_filter( 'posts_where', 'only_fetch_short_posts', 10 );
    
    function only_fetch_short_posts( $where, $query ) {
    	$where .= ' AND CHAR_LENGTH(post_content) <= 120';
    	return $where;
    }
    
    while ( $feature_query->have_posts()) : $feature_query->the_post(); ?>
      <?php the_content(); ?>
    <?php endwhile; ?>
    </div>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Random Post, Max Character Length’ is closed to new replies.