然后,我想知道一旦他们的订阅量增加,如何更改帖子状态
function downgrade_user_role( $entry, $subscription_id, $transaction_id, $new_payment_amount ) {
$user = GFUserData::get_user_by_entry_id( $entry[\'id\'] );
$user->set_role( \'subscriber\' );
global $wpdb;
$wpdb->query( $wpdb->prepare(
"UPDATE $wpdb->posts SET post_status = \'draft\' WHERE post_author = %d
AND post_status IN (\'publish\', \'future\')",
$user->ID
) );
}
add_action( \'gform_authorizenet_subscription_canceled\', \'downgrade_user_role\', 10, 4 );
另一种选择是只在查询中包含特定用户角色的帖子,但也找不到这样做的方法。
function exclude_subscribers_posts( $query ) {
if ( is_admin() || ! is_main_query() ) return;
$subscribers = get_users( array(\'role\'=>\'subscriber\', \'fields\'=>\'ID\') );
if ( $subscribers ) {
$subscribers = implode(\',\', array_map(\'intval\', $subscribers));
global $wpdb;
$exclude = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts WHERE post_author IN ($subscribers)"
);
if ( empty($exclude) ) return $query;
$include = (array) $query->get(\'post__in\');
if ( $include ) {
$query->set(\'post__in\', array_diff( $include, $exclude) );
} else {
$query->set(\'post__not_in\', $exclude);
}
}
}
add_action(\'pre_get_posts\',\'exclude_subscribers_posts\');
考虑到:
由于这些原因,选项1更快,只运行一次选项2较慢,并且在每个(主)查询上运行option 1 is much better, 只有当您出于任何原因绝对不能使用选项1时,才使用选项2。