Need code or SQL query to reorder Unit Sections in CoursePress

I would like to reorder the Unit Sections in CoursePress. Units can be reordered by drag-n-drop, but Unit Sections (pages) cannot. Can this be accomplished with a code snippet or SQL query?

  • Ivan

    Hi Bela !

    You could try adding the following snippet in a mu-plugin

    <?php
    function wpmu_coursepress_replace_two_page( $array, $old_position, $new_position ) {
    	$old = !empty( $array['page_' . $old_position ] ) ? $array['page_' . $old_position ] : '' ;
    	$new = !empty( $array['page_' . $new_position ] ) ? $array['page_' . $new_position ] : '' ;
    	$array['page_' . $old_position ] = $new;
    	$array['page_' . $new_position ] = $old;
    
    	return $array;
    }
    function wpmu_coursepress_replace_show_page_title( $array, $old_position, $new_position ) {
    	$old = !empty( $array[ $old_position - 1 ] ) ? $array[ $old_position - 1 ] : null ;
    	$new = !empty( $array[ $new_position - 1 ] ) ? $array[ $new_position - 1 ] : null ;
    	$array[ $old_position - 1 ] = $new;
    	$array[ $new_position - 1 ] = $old;
    
    	return $array;
    }
    
    function wpmu_coursepress_reorder_modules() {
    	if ( isset( $_GET['wpmu_coursepress_reorder_modules'] ) ) {
    		$unit_id = 10;
    		$old_position = 2;
    		$new_position = 3;
    
    		$page_title = get_post_meta( $unit_id, 'page_title', true );
    		$show_page_title = get_post_meta( $unit_id, 'show_page_title', true );
    		$page_description = get_post_meta( $unit_id, 'page_description', true );
    
    		update_post_meta( $unit_id, 'page_title', wpmu_coursepress_replace_two_page( $page_title, $old_position, $new_position ) );
    		update_post_meta( $unit_id, 'page_description', wpmu_coursepress_replace_two_page( $page_description, $old_position, $new_position ) );
    		update_post_meta( $unit_id, 'show_page_title', wpmu_coursepress_replace_show_page_title( $show_page_title, $old_position, $new_position ) );
    
    		global $wpdb;
    		$old_modules = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON ( p.ID = pm.post_id AND pm.meta_key='module_page' AND pm.meta_value='" . intval( $old_position ) . "' ) WHERE post_parent=" . intval( $unit_id ));
    		$new_modules = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON ( p.ID = pm.post_id AND pm.meta_key='module_page' AND pm.meta_value='" . intval( $new_position ) . "' ) WHERE post_parent=" . intval( $unit_id ));
    
    		foreach( $old_modules as $ID ) {
    			update_post_meta( $ID, 'module_page', $new_position );
    		}
    
    		foreach( $new_modules as $ID ) {
    			update_post_meta( $ID, 'module_page', $old_position );
    		}
    	}
    }
    add_action( 'init', 'wpmu_coursepress_reorder_modules' );

    Note: you should change three first variables in this snippet : $unit_id, $old_position, $new_position.
    You can change only two modules per one cycle. Let's imagine that you want to replace first and fifth modules. So, after this action, the first module will have the fifth position and the fifth module will have the first position and other modules won't change their positions. In this case, you should change these variables $old_position = 1; $new_position = 5;

    Then you should define Unit ID. You can open Unit page and open context menu by Unit and select Inspect (for Chrome) and then you should find data-tab attribute and it's Unit ID (for my case it's 10, so in this snippet I set $unit_id = 10; )

    After that, you should open {your_domain.com}/?wpmu_coursepress_reorder_modules=1 page. (each update of this page will replace these Units)

    Then you can change $unit_id, $old_position, $new_position and update {your_domain.com}/?wpmu_coursepress_reorder_modules=1 page again and it will change Unit positions again. And then yuo can repeat this cycle many times.

    Note: don't forget to delete this MU plugin after all changes!

    Best regards,
    Ivan.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.