Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Created April 24, 2020 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JarrydLong/9082406992dbb4dd68c19a8e73a9701b to your computer and use it in GitHub Desktop.
Save JarrydLong/9082406992dbb4dd68c19a8e73a9701b to your computer and use it in GitHub Desktop.
<?php
function mypmpro_bulk_assign_levels_to_posts(){
if( isset( $_REQUEST['assign_levels_posts'] ) ){
global $wpdb;
$levels = array( 1, 2 ); //Assign these levels to each post
$post_type = 'post'; //Change to your preference
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ){
while( $the_query->have_posts() ){
$the_query->the_post();
$post_id = get_the_ID();
//remove all memberships for this page
$wpdb->query("DELETE FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '$post_id'");
//add new memberships for this page
if(is_array($levels))
{
foreach($levels as $level){
$sql = "INSERT INTO {$wpdb->pmpro_memberships_pages} (membership_id, page_id) VALUES('" . intval($level) . "', '" . intval($post_id) . "')";
echo $sql."<br/>";
$wpdb->query($sql);
}
}
}
} else {
echo "Nothing Found";
}
exit();
}
}
add_action( 'admin_init', 'mypmpro_bulk_assign_levels_to_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment