Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

Thanks

share|improve this question
2  
Ever heard of the XML_RPC API? – kaiser Nov 15 '12 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 – roly Nov 15 '12 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 Nov 15 '12 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 – roly Nov 15 '12 at 15:21

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
Now using what you suggested, but again it just keeps re-adding, a bigger overview available: pastebin.com/5TAHC3XU – roly Nov 15 '12 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 Nov 15 '12 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 – roly Nov 15 '12 at 15:37
Argh... my switch is backwards. It should be !empty($cID) then do nothing. – s_ha_dum Nov 15 '12 at 15:43
You sir are a scholar and a gentleman, many many thanks to you – roly Nov 15 '12 at 16:02

If you want to find if a POST with TITLE already exists, then you can use:

method 1)

$title = 'mytitlee';

global $wpdb;
$id_ofpost_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = $title");
$id_ofpost_title = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = $title");
if ($id_ofpost_name || $id_ofpost_title) {echo 'Exists, here is the id:'.$id_ofpost_title.$id_ofpost_name;} 
else {echo 'post wasnt found';}

method 2) (this method is without Mysql query, so it may be a bit slower).

$title = 'my_slug';
//you can use post_types too
$ptype='';

global $wpdb;
global $post;
$args = array( 'post_type' => $ptype);
$allPosts = get_posts($args);
$smthh='no';
foreach( $allPosts as $page )
{
    if( $smthh == 'no' && ($page->post_name == $title ||  $page->post_title == $title) )
    {$smthh='yes'; break; }
}


if ( $smthh == 'yes' )
{ 
    echo "yes, post was found";
}

else
{
    echo "no, not found";

    // You can insert the post too
    $my_post = array( 'post_type'    => $ptype, 'post_title'    => $title, 'post_content'  => 'This is my post content.', 'post_status'   => 'publish', );
    wp_insert_post( $my_post );
}
share|improve this answer
How do you get a global $post object in an importer? – toscho Apr 8 at 12:55
3  
Why are you copy/pasting this answer? wordpress.stackexchange.com/a/94998/21376 – s_ha_dum Apr 8 at 13:12
I have copied, because from google, people will find it easily. – Solutioner Apr 8 at 13:24
2  
Please, make your answers unique, if the same solution applies to 2 different Questions, it means they are duplicates and one of them should be flagged for closing. Check the pages about and How to Answer. Once you have a bit more of reputation, you'll be able to add comments to Questions and Answers, and make a point "Hey, look at this solution I'm proposing in this other Q&A". – brasofilo Apr 8 at 14:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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