• Resolved mattgooch

    (@mattgooch)


    Hi, Need a little help, i want to check if an ID exisits in WP before creating a post.

    IF (Post ID already exists) Then {
    Dont post anything
    } ELSE {
    Create a new post

    I have the code which creates the posts, any ideas from here on:

    `$my_post = array(
    ‘ID’ => $tumblrID,
    ‘post_title’ => $title,
    ‘post_content’ => $post,
    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    ‘tags_input’ => $tags,
    ‘post_category’ => array(8,39)

    );

    wp_insert_post( $my_post );`

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    http://codex.wordpress.org/Function_Reference/wp_insert_post
    From that page:

    IMPORTANT: Setting a value for $post[‘ID’] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post[‘ID’] must be blank or not set at all.

    WordPress creates a new ID when inserting new posts.

    To check if a post ID exists you can use this:

    <?php
    	global $wpdb;
    	$post_id = 509;
    	$post_exists = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE id = '" . $post_id . "'", 'ARRAY_A');
    	if($post_exists){
    	  echo 'post_id exists';
    	} else {
    	  echo 'post_id does not exist';
    	}
    ?>

    Thread Starter mattgooch

    (@mattgooch)

    Thanks that works well, i realise you cant set the ID, have to change my plan but this will come in very handy thanks.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Check Post ID exisits before posting’ is closed to new replies.