Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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