首页 > 解决方案 > WP - 仅在帖子不存在时插入帖子

问题描述

背景:自定义帖子(类型:事件)可以由用户手动添加(在这种情况下不存在 hq_id 元数据,或者由 wp_insert_post() 自动(从另一个来源提取)(在这种情况下存在 hq_id)。

有时帖子可以有通用标题,因此在插入帖子之前检查是否存在特定标题的帖子是不够的。下面的思路是:

在尝试从其他来源插入帖子并将其与用户手动添加的帖子合并之前,请检查以下内容:

  1. 是否存在同名帖子?a) 否 => 让我们插入它 (End) b) 是的。

  2. 如果是:它是否具有 hq_id 元数据并且这与要插入的帖子的 hq_id 元数据相等。

a) 是 => 好的,这是重复的帖子,无需插入

b) 否 => 存在相同标题的帖子,但 hq_id 不存在或不同,因此它不是重复的。让我们插入它。

这很好,直到您再次运行代码。每次似乎都在添加满足 2b 条件的帖子。

对于代码的每次重新运行,2a 和 2b 都为真。我不确定为什么它在 2a 之后没有退出 if 语句,但它仍然在 2b 中退出。

此外,正如您将看到的,整个 $new_post/wp_insert_post 代码块应该被移动到一个函数中,因为它被使用了两次。我应该把函数定义放在哪里,这样我就不会遇到范围问题?

这是代码:

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>';
  }

标签: wordpressif-statement

解决方案


我稍微改变了方法并解决了如下问题:

if( ! empty( $data_events ) ) {
    function add_event($title, $id, $uri, $event_date) {
      $new_post = array(
        'post_title' => $title,
        'post_content' => 'description',
        'post_status' => 'publish',
        'post_author' => '2',
        'post_type' => 'event',
        'meta_input' => array(
          'hq_id' => $id,
          'hq_uri'=> $uri,
          'event_date' => $event_date,
        )
      );
      $pid = wp_insert_post($new_post);
      wp_set_object_terms($pid, 'riderhq', 'event_category');
      $get_meta_time = get_post_meta($pid, 'event_date');
      $newformat = date('Ymd', strtotime($get_meta_time[0]));
      update_post_meta($pid, 'event_date', $newformat);    
    }

      foreach( $data_events as $event ) {

      $existing_posts_arguments = array(
        'hierarchical' => 1,
        'meta_key' => 'hq_id',
        'meta_value' => $event['id'],
        'post_type' => 'event',
      );

      $existing_posts = get_posts( $existing_posts_arguments );

      if ( count($existing_posts) < 1 ) {
        add_event($event['name'], $event['id'], $event['uri'], $event['start_date']); 
      }


    } // end of foreach event 
  }

推荐阅读