Support » Plugin: Post Type Switcher » Resolved bug I believe with WPML, ACF , etc..

  • Resolved bug with ACF file field.

    
    public function override_type( $data = array(), $postarr = array() ) {
    global $post;
    if ( !empty( $post ) && $post->ID !== $postarr['ID'] ) {
        return $data;
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • I can confirm this solves a conflict I encountered with ACF.

    I’m running code on the acf/save_post action to update the definition of an ACF field. ACF fields are saved as posts, and when the field is updated, its post_type gets changed from acf-field to the post_type of the post using the field.

    Please update! PTS is great but ACF is critical to the functioning of the site this is happening on.

    Jonathan Cowher

    (@webgeekconsulting)

    Building off @hrohh’s logic… this will also work when bulk editing…

    
    public function override_type( $data = array(), $postarr = array() ) {
    
    		// Bail if form field is missing
    		if ( empty( $_REQUEST['pts_post_type'] ) || empty( $_REQUEST['pts-nonce-select'] ) ) {
    			return $data;
    		}
    
    		// Get post IDs
    		$post_ids = array();
    		if ( isset( $_REQUEST['bulk_edit'] ) && ! empty( $_REQUEST['post'] ) ) {
    			$post_ids = array_map( 'intval', (array) $_REQUEST['post'] );
    		} elseif ( ! empty( $_REQUEST['post_ID'] ) ) {
    			$post_ids = array( intval( $_REQUEST['post_ID'] ) );
    		}
    
    		// Bail if post ID is invalid
    		if ( ! ( $post_ids && in_array( $postarr['ID'], $post_ids ) ) ) {
    			return $data;
    		}
    
    		// Post type information
    		$post_type        = sanitize_key( $_REQUEST['pts_post_type'] );
    		$post_type_object = get_post_type_object( $post_type );
    
    		// Bail if empty post type
    		if ( empty( $post_type ) || empty( $post_type_object ) ) {
    			return $data;
    		}
    
    		// Bail if user cannot 'edit_post'
    		if ( ! current_user_can( 'edit_post', $postarr['ID'] ) ) {
    			return $data;
    		}
    
    		// Bail if nonce is invalid
    		if ( ! wp_verify_nonce( $_REQUEST['pts-nonce-select'], 'post-type-selector' ) ) {
    			return $data;
    		}
    
    		// Bail if autosave
    		if ( wp_is_post_autosave( $postarr['ID'] ) ) {
    			return $data;
    		}
    
    		// Bail if revision
    		if ( wp_is_post_revision( $postarr['ID'] ) ) {
    			return $data;
    		}
    
    		// Bail if it's a revision
    		if ( in_array( $postarr['post_type'], array( $post_type, 'revision' ), true ) ) {
    			return $data;
    		}
    
    		// Bail if user cannot 'publish_posts' on the new type
    		if ( ! current_user_can( $post_type_object->cap->publish_posts ) ) {
    			return $data;
    		}
    
    		// If post type has changed...
    		if ( $data['post_type'] !== $post_type ) {
    			// Set post parent to 0
    			$data['post_parent'] = 0;
    			// Update post type
    			$data['post_type'] = $post_type;
    		}
    
    		// Return modified post data
    		return $data;
    	}
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Resolved bug I believe with WPML, ACF , etc..’ is closed to new replies.