Yes $wpdb
is built in and is loaded by the WordPress Core on every page load.
I don’t see any critical problem with your code. I am going to suggest some improvements because I see some places where could go wrong, but mostly that should work. That makes me think that $article->heading
is not matching up to the inserted post_title
. So…
Try to normalize your title with sanitize_title_with_dashes
which is how WordPress builds the post slug, and then check for that slug instead.
global $wpdb;
$query = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_name = %s', sanitize_title_with_dashes($article->heading));
$cID = $wpdb->get_var( $query );
Now your query is looking for post_name
, which is the slug or permalink tail, and I changed $wpdb->query
to $wpdb->get_var
since you are looking for a single variable, which we save to $cID
.
Use a better check for your conditional.
if ( !empty($cID) ) {
However, this might not be the right approach at all. Look into the XML_RPC API mentioned by @kaiser. It has post insertion capabilities.