samedi 22 septembre 2018

WP - insert post only if it does not exist

Background: The custom posts (type: Event) can be added manually by a user (in that case no hq_id meta data is present, or automatically (pulled from another source) by wp_insert_post() (in that case hq_id is present).

Some times the posts can have generic titles so before inserting a post checking if a post of a particular title exists is not sufficient. The idea below is:

Before trying to insert a post from another source and merge it with posts added manually by users the following gets checked:

  1. Does a post of the same title exists? a) No => Let's insert it (End) b) Yes.

  2. If Yes: does it have the hq_id meta data and is this equal with hq_id meta of the post that is to be inserted.

a) Yes => ok, that's a duplicate post, no need to insert it

b) No => a post by the same title exists, but the hq_id does not exist or is different, so it's not a duplicate. Let's insert it.

That is fine, until you run the code again. Each time it seems to be adding the posts meeting the 2b condition.

For each re-run of the code, both 2a and 2b are true. I'm not sure why it doesn't exit the if statement after 2a but it still does 2b.

Also, as you'll see the whole $new_post/wp_insert_post code block should be moved to a function as it's being used twice. Where would I put the function definition so I don't have problems with scope?

Here's the code:

if( ! empty( $data_events ) ) {
    echo '<ul>';
        foreach( $data_events as $event ) {

        $title_exists = get_page_by_title( $event['name'], OBJECT, 'event');

        if ( $title_exists == null ) {
          echo 'Post of the same title does not exist - we need to insert post';
          $new_post = array(
            'post_title' => $event['name'],
            'post_content' => 'desssscccd',
            'post_status' => 'publish',
            'post_author' => '2',
            'post_type' => 'event',
            'meta_input' => array(
              'hq_id' => $event['id'],
              'hq_uri'=> $event['uri'],
              )
          );
          $pid = wp_insert_post($new_post);
          wp_set_object_terms($pid, 'riderhq', 'event_category');


        } else { // A post of the same title exists
          $my_post = get_page_by_title ( $event['name'], OBJECT, 'event' );
          $post_hq_id = $my_post->hq_id;
          if ( $post_hq_id && $post_hq_id == $event['id'] ) {
            echo "post of the same title exists and has the same hq_id - no need to insert it";
          } else {
            echo "post of the same title exists but doesnt have the same hq_id - we need to insert it";
            $new_post = array(
              'post_title' => $event['name'],
              'post_content' => 'description',
              'post_status' => 'publish',
              'post_author' => '2',
              'post_type' => 'event',
              'meta_input' => array(
                'hq_id' => $event['id'],
                'hq_uri'=> $event['uri'],
              )
          );
          $pid = wp_insert_post($new_post);
          wp_set_object_terms($pid, 'riderhq', 'event_category');

          }
        }

        echo '<li>';
        echo $event['id'];
                echo '<a href="' . esc_url( $event['uri'] ) . '">' . $event['name'] . '</a>';
        echo '</li>';

        }
      echo '</ul>';
  }

Aucun commentaire:

Enregistrer un commentaire