1

I am using the code below to pull a random post of my site every 6 hour.

if ( false === ( $social_trans_post_id = get_transient( '$social_trans_post_id' ) ) ) {
                $args = array('numberposts' => 1, 'orderby' => 'rand');
                $social_trans = get_posts($args);
                set_transient('$social_trans_post_id', $social_trans[0]->ID, (60*60*6));
            } else {
                $args = array('post__in' => array($social_trans_post_id));
                $social_trans = get_posts($args);
            }

To integrate with Twitter, I'd like to only pull posts with 140 characters or less.

I know about <?php echo substr(($post->post_content), 0,140); ?> which I am using already as a temporary solution.

I tried to setup a condition and this is where I got stuck:

if ( false === ( $social_trans_post_id = get_transient( '$social_trans_post_id' ) ) ) {
                $args = array('numberposts' => 1, 'orderby' => 'rand');
                $social_trans = get_posts($args);
                        if (strlen( $social_trans->post_content > 140 ) ) {
                           // Pull another post? 
                        }
                set_transient('$social_trans_post_id', $social_trans[0]->ID, (60*60*6));
            } else {
                $args = array('post__in' => array($social_trans_post_id));
                $social_trans = get_posts($args);
            }
2
  • 2
    You are missing a ). If you had debugging enabled you would spot those errors pretty quick.
    – s_ha_dum
    Commented Mar 22, 2014 at 22:34
  • Thanks. I wrote that part for the sake of this question, didn't actually run the code as it was still uncompleted.
    – Tony Fire
    Commented Mar 23, 2014 at 2:35

1 Answer 1

4

I want to suggest you another approach.

You can set a meta when you saving a post if its content is less than 140 chars, so then you can simply runs a simple meta query.

add_action( 'save_post_post', function( $id, $post ) {
  if ( $post->post_status !== 'publish' ) {
    return;
  }
  if ( strlen( $post->post_content ) <= 140  ) {
    update_post_meta( $id, '_under_140', 1 );
  }
}, 10, 2 );

After that your code becomes something like:

function get_social_trans_post() {

  $social_trans_post = get_transient( 'social_trans_post' );

  if ( empty( $social_trans_post ) ) {
    $args = array( 'posts_per_page' => 1, 'meta_key'=>'_under_140', 'orderby'=>'rand' );
    $social_trans_posts = get_posts( $args );
    if ( ! empty( $social_trans_posts ) ) {
      $social_trans_post = $social_trans_posts[0];
      set_transient( 'social_trans_post', $social_trans_post, 6 * HOUR_IN_SECONDS );
    }
  }
  return $social_trans_post;
}

And where you need the post just call get_social_trans_post().

Worth noting that I saved in transient the whole post, not only the id, in this way there is no need to query for post, after get the id. All the serialization / unserialization work is done by WordPress, and what you get is exactly what you stored.

Off course this code will work for posts that you add or update after adding it, but you can run a run-once script to set the meta on your recents posts having the wantend content length.

5
  • Didn't update the post because I was still experimenting, but I did end up with a similar solution. Realized that identifying all wanted posts first through meta key is a much simpler approach. Would have saved me from an headache if I saw this earlier.
    – Tony Fire
    Commented Mar 23, 2014 at 2:28
  • For some strange reason, using Save_Post and Save_Post_Post added the meta_key to all my new posts, before I even posted them. I replaced it with publish_post and it works now.
    – Tony Fire
    Commented Mar 23, 2014 at 11:51
  • @TonyFire yep, you're right, save_post run even when the post is in just created (auto-draft) and the content is empty, so less then 140 chars. I'm updating the answer.
    – gmazzap
    Commented Mar 23, 2014 at 13:45
  • Thanks, what's the difference between using save_post action hook with the publish post condition and using publish_post action hook directly?
    – Tony Fire
    Commented Mar 23, 2014 at 16:00
  • @TonyFire not much really.
    – gmazzap
    Commented Mar 23, 2014 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.