0

I have a CPT called Contest. When a contest is created or updated, I want to manually set the value of the post content to my shortcode from my plugin. I have custom metaboxes that I use with this CPT, so the content of the post will be based on the values of those metaboxes. I have tried add_filter('the_content', 'my_content') but no changes are made to the post_content column in the database. What am I doing wrong?

Note: I used wp_die() to see if the filter was even being called, and wp_die() didn't do anything.

Here is my code:

function imgw_meta_boxes(){
    add_meta_box( 'contest-dates', __('Running Dates'), 'imgw_dates_metabox', 'contest' );
    add_meta_box( 'contest-description', __('Description/Rules'), 'imgw_description_editor', 'contest' );
}
add_action( 'add_meta_boxes', 'imgw_meta_boxes' );

function imgw_dates_metabox($object){

      wp_nonce_field(basename(__FILE__), "meta-box-nonce");
?>
<div>
<label>Starting Date</label>
<input type="date" name="contest-starting-date" value="<?php echo get_post_meta($object->ID, "contest-starting-date", true); ?>"> at <input type="time" name="contest-starting-time" value="<?php echo get_post_meta($object->ID, "contest-starting-time", true); ?>">
<label>Ending Date</label>
<input type="date" name="contest-ending-date" value="<?php echo get_post_meta($object->ID, "contest-ending-date", true); ?>"> at <input type="time" name="contest-ending-time" value="<?php echo get_post_meta($object->ID, "contest-ending-time", true); ?>">
</div>

<?php
} 

function imgw_description_editor($object){ ?>
<textarea name="contest-description" rows="10" cols="100" placeholder="Enter a description or rules..."><?php echo get_post_meta($object->ID, 'contest-description', true); ?></textarea>
<?php    
}
function imgw_save_contest_meta( $post_id, $post ) {

    global $post;

  if($post->post_type == 'contest')
    return;

    update_post_meta( $post_id, 'contest-description', $_POST['contest-description'] );
    update_post_meta( $post_id, 'contest-starting-date', $_POST['contest-starting-date'] );
    update_post_meta( $post_id, 'contest-starting-time', $_POST['contest-starting-time'] );
    update_post_meta( $post_id, 'contest-ending-date', $_POST['contest-ending-date'] );
    update_post_meta( $post_id, 'contest-ending-time', $_POST['contest-ending-time'] );

} // end save_meta_data

add_action('save_post', 'imgw_save_contest_meta', 10, 3);

function imgw_contest_content( $content ){

    global $post;

    if($post->post_type == 'contest'){
        $content = "[imgw-contest][/imgw-contest]";
        return $content;  

    }      

    wp_die('Hello!');

}

add_filter('the_content', 'imgw_contest_content');
  • the_content only in action when you are viewing post on front-end no changes will be made on DB or actual post content! – Sumit Mar 19 '16 at 16:35
  • if you're going to override the post's content, just hide the field in your post type registration and output what you need on the front end via filters. I don't see any advantage to adding this complexity to the back end. – Milo Mar 19 '16 at 16:38
  • Also, your wp_die() isn't doing anything because you're using return before you get there. – Pat J Mar 19 '16 at 21:54
0

To update properties of a post in the database use wp_update_post.

// Update post 37
  $my_post = array(
      'ID'           => 37,
      'post_title'   => 'This is the post title.',
      'post_content' => 'This is the updated content.',
  );

// Update the post into the database
  wp_update_post( $my_post );

Just beware of the possibility of an infinite loop when updating a post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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