Created
January 28, 2016 11:25
-
-
Save garvs/93894e60e0c51d68b2f6 to your computer and use it in GitHub Desktop.
Bulk update posts by ID list (category) to set User Role Editor Pro content view restrictions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Bulk update posts by ID list to set User Role Editor Pro content view restrictions | |
* | |
*/ | |
add_action('admin_init', 'URE_Content_View_Restrictons_Bulk::update'); | |
class URE_Content_View_Restrictons_Bulk { | |
const prohibit_allow_flag = 2; // Allow | |
const content_for_roles = 'abc'; // comma separated roles ID list | |
const post_access_error_action = 1; // HTTP 404 error | |
const post_access_error_message = ''; // error message | |
const categories = '55'; // comma separated list of categories | |
private static function get_posts_list() { | |
//$list = array(1260, 1261, 1262); | |
global $wpdb; | |
$query = 'SELECT object_id FROM '. $wpdb->prefix .'term_relationships where term_taxonomy_id in ('. self::categories .')'; | |
$list = $wpdb->get_col($query); | |
return $list; | |
} | |
// end of get_posts_list() | |
private static function update_post_meta($post_id) { | |
update_post_meta($post_id, URE_Content_View_Restrictions::prohibit_allow_flag, self::prohibit_allow_flag); | |
update_post_meta($post_id, URE_Content_View_Restrictions::content_for_roles, self::content_for_roles); | |
update_post_meta($post_id, URE_Content_View_Restrictions::post_access_error_action, self::post_access_error_action); | |
update_post_meta($post_id, URE_Content_View_Restrictions::post_access_error_message, self::post_access_error_message); | |
} | |
// end of update_post_meta() | |
public static function update() { | |
if (!defined('URE_PLUGIN_DIR')) { | |
return; | |
} | |
require_once( URE_PLUGIN_DIR .'includes/pro/classes/content-view-restrictions.php'); | |
$posts_list = self::get_posts_list(); | |
$last_post_in_list = $posts_list[count($posts_list)-1]; | |
$roles = get_post_meta($last_post_in_list, URE_Content_View_Restrictions::content_for_roles, true); | |
if ($roles==self::content_for_roles) { // already done - do not execute bulk update twice | |
return; | |
} | |
foreach($posts_list as $post_id) { | |
self::update_post_meta($post_id); | |
} | |
} | |
// end of ure_bulk_content_view_restrictions_update() | |
} | |
// end of URE_Content_View_Restrictons_Bulk class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More information at the end of this post
https://www.role-editor.com/woocommerce-products-restrict-view-by-category/