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.
Guys please someone help me.
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
misiek303
Member
Posted 3 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
kgrogan
Member
Posted 3 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');
}
kgrogan
Member
Posted 3 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");
}
praskuma
Member
Posted 2 years ago #
duncanmoo
Member
Posted 2 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');
}