如何通过标题和帖子类型检查帖子是否存在

时间:2018-03-05 04:20:10

标签: wordpress posts

我正在尝试在创建事件(自定义帖子类型)时添加新的博客帖子,并在事件更新时编辑帖子。到目前为止,我已经能够在创建事件时添加新帖子,但是当事件被保存时它也会创建一个新帖子,因此我需要在插入之前检查博客中是否已存在帖子。我尝试了post_exists(title)函数,但后来它完全停止创建帖子所以我认为问题是我需要一种方法来检查特定帖子类型是否存在帖子?

这是我到目前为止所做的:

<RelativeLayout
    android:id="@+id/fragment_camera"
    android:layout_width="200dp"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true"
    android:gravity="bottom">

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

我认为您最好使用自定义字段来检查我会稍微更改您的代码

    $post_child_id = get_post_meta($post_id, 'child-post', true);
     if (!$post_child_id) {
        $post_child_id = wp_insert_post(
        array(
            'comment_status'    =>  'closed',
            'ping_status'       =>  'closed',
            'post_author'       =>  $author_id,
            'post_title'        =>  $post_title,
            'post_content'      =>  $post_content,
            'post_status'       =>  'publish',
            'post_type'     =>  'post'
        )

    );
  update_post_meta( $post_id, 'child-post', $post_child_id);
 }

答案 1 :(得分:0)

现在就开始工作了。结束使用get_page_by_title()功能并将其传递给&#39;发布&#39;作为帖子类型,然后检查它是否为空。

这是我提出的最终解决方案:

add_action( 'save_post', 'create_event_post' );

function create_event_post( $post_id ) {

    // Set the title, thumbnail id, author, and content variables
    $post_title = get_the_title( $post_id );
    $post_type = get_post_type($post_id);
    $post_content = get_post_field('post_content', $post_id);
    $thumbnail_id = get_post_thumbnail_id( $post_id );
    $author_id = get_post_field ('post_author', $post_id);

    // If the post is not "tribe_events", don't create a new post.  
    if ( "tribe_events" != $post_type ) 
        return;

    $new_post = array(
                'comment_status'    =>  'closed',
                'ping_status'       =>  'closed',
                'post_author'       =>  $author_id,
                'post_title'        =>  $post_title,
                'post_content'      =>  $post_content,
                'post_status'       =>  'publish',
                'post_type'     =>  'post'
            );

    remove_action( 'save_post', 'create_event_post' );

    $post_exists = get_page_by_title( $post_title, $output, "post" );

    if ( !empty($post_exists) ) {
        // Update post
        $update_post = array(
            'ID'           =>   $post_exists->ID,
            'post_title'   =>   $post_title,
            'post_content' =>   $post_content,
        );

        // Update the post into the database
        wp_update_post( $update_post );
        set_post_thumbnail( $post_exists->ID, $thumbnail_id );
    }
    else {
        // Create the new post and retrieve the id of the new post
        $new_post_id = wp_insert_post ( $new_post );
        // Set the featured image for the new post to the same image as event post 
        set_post_thumbnail( $new_post_id, $thumbnail_id );
    }           

    // Now hook the action
    add_action( 'save_post', 'create_event_post' );
}