WordPress.org

Ready to get started?Download WordPress

Forums

How to check if a post exists by title (8 posts)

  1. Oren Yomtov
    Member
    Posted 5 years ago #

    How can I check if a post exists with a certain title?
    e.g.:if (!wp_post_exists('Hello World!')) wp_create_post(.....);

    Is there any function like wp_post_exists? or maybe a way to search the posts by title. or put all post names in an array.

    Thanks in advance, Oren.

  2. Oren Yomtov
    Member
    Posted 5 years ago #

    Guys please someone help me.

  3. whooami
    Member
    Posted 5 years ago #

    49 mins before bumping? :( bad style

    this forum has a page that highlights unanswered posts. By impatiently bumping this, youve moved this thread off that page.

    a little advice for next time, try to be patient and dont bump

  4. misiek303
    Member
    Posted 5 years ago #

    add this to your plugin somewhere

    function wp_exist_post($id) {
    global $wpdb;
    return $wpdb->get_row("SELECT * FROM wp_posts WHERE id = '" . $id . "'", 'ARRAY_A');
    }

    that bizare that wordpress hasn't it

  5. kgrogan
    Member
    Posted 5 years ago #

    The original question asked about finding the post from its title, so here is a version of the above function to find a post by title:

    function wp_exist_post_by_title($title_str) {
    global $wpdb;
    return $wpdb->get_row("SELECT * FROM wp_posts WHERE post_title = '" . $title_str . "'", 'ARRAY_A');
    }
  6. kgrogan
    Member
    Posted 5 years ago #

    Since this was also asked, here is a function that returns an array of all post titles:

    function wp_get_all_post_titles() {
    global $wpdb;
    return $wpdb->get_col("SELECT post_title FROM $wpdb->posts");
    }
  7. praskuma
    Member
    Posted 4 years ago #

    Thanks this worked.

  8. duncanmoo
    Member
    Posted 4 years ago #

    Thanks for this I am a real newbie on WP.

    But I would qualify your query a little so that you only display this conditional data if the page is in fact published.

    My example below returns the ID of a page and I am limiting it to child pages of post_parent=17:

    function wp_exist_page_by_title($title_str) {
    	global $wpdb;
    	return $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_title = '" . $title_str . "' && post_status = 'publish' && post_type = 'page' && post_parent=17", 'ARRAY_N');
    }

Topic Closed

This topic has been closed to new replies.

About this Topic