WordPress.org

Forums

Check post title if exists before posting programmatically (1 post)

  1. waltone57
    Member
    Posted 9 months ago #

    Hello,

    for a couple days now, I've been trying to figure out a code that checks post title if it already exists before posting. This is what I have done now:

    function postit() {
    $user_info = wp_get_current_user();
    $title = $user_info->user_login;
    $page = get_page_by_title('$title');
    
    if(!$page){
    $my_post = array(
      'post_title'    => $title,
      'post_content'  => '[ratingz]',
      'post_status'   => 'publish',
      'post_author'   => $user_ID
    );
    wp_insert_post( $my_post );
    }
    }

    I'm using logged in user as post title.

    I think I'm not so far away but where is the problem?

    Thanks.

Reply

You must log in to post.

About this Topic