7

I have a wordpress site that connects to a soap server. The problem is every time I run the script the wp_insert_post is using the same result again.

I would like to check if existing post_title matches the value from $title then if they match, prevent wp_insert_post from using the same value again.

Here's the code:

try {
    $client = new SoapClient($wsdl, array('login' => $username, 'password' => $password));
    } catch(Exception $e) {
      die('Couldn\'t establish connection to weblink service.');
    }
$publications = $client->GetPublicationSummaries();
foreach ($publications->GetPublicationSummariesResult->PublicationSummaries->PublicationSummary as $publication_summary) {

    // get the complete publication from the webservice
    $publication = $client->getPublication(array('PublicationId' => $publication_summary->ID))->GetPublicationResult->Publication;
    
    // get all properties and put them in an array
    $properties = array();
    foreach ($publication->Property as $attribute => $value) {
        $properties[$attribute] = $value;
    }
    
    // Assemble basic title from properties
    $title = $properties['Address']->Street . ' ' . $properties['Address']->HouseNumber . $properties['Address']->HouseNumberExtension . ', ' . $properties['Address']->City->_;
}

$my_post = array(
    'post_title'=>$title,
    'post_content'=>'my contents',
    'post_status'=>'draft',
    'post_type'=>'skarabeepublication',
    'post_author'=>1,
);
wp_insert_post($my_post);

Thank you for any help.

Ruvee
  • 8,611
  • 4
  • 18
  • 44
Jay Bryan Cañas
  • 225
  • 2
  • 4
  • 10
  • 2
    you should try this code. require( dirname(__FILE__) . '/wp-load.php' ); global $wpdb; echo $count = $wpdb->get_var("select COUNT(*) from $wpdb->posts where `post_title` like '$title' "); – Robot Oct 05 '11 at 05:19

4 Answers4

16

You can use get_page_by_title() as it supports custom post types now.

if (!get_page_by_title($title, OBJECT, 'skarabeepublication')) :

    $my_post = array(
        'post_title'=>$title,
        'post_content'=>'my contents',
        'post_status'=>'draft',
        'post_type'=>'skarabeepublication',
        'post_author'=>1,
    );
    wp_insert_post($my_post);

endif;

Codex information here

CookiesForDevo
  • 711
  • 7
  • 11
  • 2
    I like this method for taking advantage of built-in functions for the best results. I believe the first line should be: "if (! get_page_by_title($title, OBJECT, 'skarabeepublication') ) :" – Jake Apr 23 '13 at 06:44
  • `OBJECT` shouldn't have apostrophes, but otherwise this method works perfectly and is still valid as of WordPress 4.7.3. – Arinthros Mar 20 '17 at 13:59
  • It will also returning the draft and trash posts. – Mayank Dudakiya Jul 19 '19 at 11:15
7

Surprised not to see mention of post_exists function in wp-includes/post.php. See entry on wpseek. There is no entry in the codex. At it's simplest it works like get_page_by_title but returns a post id (or 0 if not found) instead of the object (or null).

$post_id = post_exists( $my_title );
if (!$post_id) {
    // code here
}
Jon
  • 91
  • 1
  • 3
  • There is now an [entry on post_exists](https://developer.wordpress.org/reference/functions/post_exists/) in the Wordpress Developer Code Reference. – Jon Sep 04 '17 at 16:47
4

Sorry for the late response. I used what Robot says in the comment and this solved my problem. Thanks

$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '$title_from_soap'");
if($post_if < 1){
    //code here
}
Jay Bryan Cañas
  • 225
  • 2
  • 4
  • 10
0

sampler:

if( !get_page_by_path('mypageslug',OBJECT,'post') ){
  //your codes
}
T.Todua
  • 53,146
  • 19
  • 236
  • 237