I'm writing a plugin that takes an xml feed and posts it into WordPress using wp-cron

Everything is working dandy, except it keeps adding the same posts over again.

So, I'm working on a system to check to see if a post of that title already exists.

I've written this, but its always returning 'already exists'

global $wpdb;
        $query = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = %s', $article->heading);
        $wpdb->query( $query );

        if ( $wpdb->num_rows ) {
            error_log('already exists');
        } else {                
            wp_insert_post($post, $wp_error);

I'm fairly new to working with the database side of WordPress and I saw the $wpdb when googling for help. Now, I haven't defined this anywhere but the place I was reading seemed to suggest it was built in.

All help appreciated!


  • 2
    Ever heard of the XML_RPC API?
    – kaiser
    Commented Nov 15, 2012 at 14:35
  • I originally started this task with XML_RPC but my manager has asked me to instead develop it as a plugin so he can re-use/sell it
    – rogy
    Commented Nov 15, 2012 at 15:09
  • 2
    And why can't he do that with XML_RPC?? At least it's an API. And why would you want to sell something that doesn't align with the product that its built upon?
    – kaiser
    Commented Nov 15, 2012 at 15:11
  • Yes your right, but there were other reasons it was a bit easier this way was because i also have to convert the category names given in this feed to category ID's relative to the wordpress categories, which was looking to be a bit of a pain externally
    – rogy
    Commented Nov 15, 2012 at 15:21
  • There is one WordPress plugin Duplicate Title Checker which provides alert message that post title is unique or duplicate while entering new post. The answer given by "s_ha_dum" is correct but I am posting here because it may be useful to someone else.
    – user44535
    Commented Dec 31, 2013 at 13:56

2 Answers 2


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.

  • Now using what you suggested, but again it just keeps re-adding, a bigger overview available: pastebin.com/5TAHC3XU
    – rogy
    Commented Nov 15, 2012 at 15:15
  • Add wp_die($cID); after your query, or log it some other way. Are you getting an ID? Also, what is the point of this: ` $allPosts = get_posts($opts);? You aren't using $allPosts` at all.
    – s_ha_dum
    Commented Nov 15, 2012 at 15:29
  • $cID printed out a number that was increcementing by 1 after each iteration, also good spot removed that now was for something I was trying, thank you
    – rogy
    Commented Nov 15, 2012 at 15:37
  • Argh... my switch is backwards. It should be !empty($cID) then do nothing.
    – s_ha_dum
    Commented Nov 15, 2012 at 15:43
  • You sir are a scholar and a gentleman, many many thanks to you
    – rogy
    Commented Nov 15, 2012 at 16:02
<?php post_exists( "post_name" ); ?>


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.