0

我正在使用 Geolocation IP Detection 插件来检测访问者所在的国家/地区。我在帖子编辑器页面上有自定义分类的元框,可以添加国家/地区名称。

因此,如果用户不是来自分类国家/地区,我们希望将帖子变为用户的私人帖子。

这是我的功能,但它没有按预期工作。请帮帮我!

function geo_content_change_post_visibility( $post ) {

    $geoip = geoip_detect2_get_info_from_current_ip();
    $name = $geoip->raw['country']['names']['en'];

    $terms = get_the_terms( $post->ID, 'country' );

    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        $terms_string = wp_list_pluck($terms, 'name');
    }

    if ( get_post_status ( $post->ID ) == 'public' ) {
        
        $post_data = array(
            'ID' => $post->ID,
            'post_status' => 'private'
        );
    
        if ( ! in_array( $name, $terms_string )) {
            if ( ! is_admin() ){
                wp_update_post( $post_data );
            }
        }

    }

    return $post;
}

add_filter( 'the_post', 'geo_content_change_post_visibility' );
4

0 回答 0