使用帖子类别“ A”(或“ B”或“ C”)创建新的Wordpress帖子时,我需要使用PHP自动选中Woocommerce-Memberships Disablestrict 复选框钩。这将允许所有查看者无限制地访问该帖子。
checkbox picture [1]
我尝试将操作添加到“ save_post”钩子中,以将“ yes”的“ _wc_memberships_force_public”值添加到{wp_prefix} _post_meta表中,因此,此复选框显示为选中状态,但是,在查看帖子时它仍然受到限制。当我删除操作(通过从我的functions.php文件中将其注释掉)并在保存帖子之前手动选中禁用限制复选框时,可以不受限制地(按预期)查看帖子。>
add_action( 'save_post', 'action_save_post_force_public', 99, 3);
function action_save_post_force_public($post_id, $post, $is_update){
$already_forced = get_post_meta( $post_id, '_wc_memberships_force_public', $single=true );
if ( 'yes' === $already_forced ){ return; }
$categories = get_the_category( $post_id );
if ( empty( $categories ) ){ return; }
$force_public_slugs = array('A', 'B', 'C');
foreach ( $categories as $term ){
if ( in_array( $term->slug, $force_public_slugs ) ){
update_post_meta( $post_id, '_wc_memberships_force_public', 'yes' );
break;
}
}
}
我希望所有查看者都可以无限制地查看帖子内容,无论此框是自动还是手动选中。相反,它仅在手动选中该框时有效。
答案 0 :(得分:0)
尝试使用以下方法代替使用update_post_meta( $post_id, '_wc_memberships_force_public', 'yes' );
:
// Disable post restrictions (Woocommerce Membership)
$wc_membership = \WC_Memberships::instance();
$wc_membership->get_restrictions_instance()->set_content_public($post);
对我来说很好;)
您可以在文件woocommerce-memberships/includes/admin/class-wc-memberships-admin.php