• Hello, I have set up CPT-onomies so that I can assign a category selection to images (attachments) which I upload to WordPress. My intention to be able to display a gallery of images which can filtered in accordance with the categories assigned to the images.

    I have used the following code in my functions.php file to attach / enable an existing a CPT-onomies which I have setup for use with attachments.

    add_action( 'wp_loaded', 'my_website_register_cpt_onomy' );
    function my_website_register_cpt_onomy() {
       global $cpt_onomies_manager;
       if ( $cpt_onomies_manager ) {
          $cpt_onomies_manager->register_cpt_onomy( 'bathroom_categories', 'attachment' );
       }
    }

    The above code now displays the ‘bathroom_categories’ selection box on the media editing page however I have encountered a problem whereby when I select categories to assign to the image and then update the post it doesn’t save the selection I had made??

    A 2nd problem I have also noticed is that now when I access the CPT-onomies admin panel and the ‘bathroom_categories’ CPT it tells me “This custom post type is being programmatically registered as a CPT-onomy, which overrides any settings defined below.”

    Both problems maybe related as this is new ground for me so I’m hoping for some advice from those in the know if possible?! In the mean time i will continue to read more and see if i get to grips with it myseldf.

    Ideally I’d like to be able to set up the CPT via the CPT-onomies admin control panel and have it availble to use / assign to attachements as well as posts if possible.

    Looking forward to any help people can offer.

    Regards

    James

    https://wordpress.org/plugins/cpt-onomies/

Viewing 2 replies - 1 through 2 (of 2 total)
  • +1

    I’m having the exact same problem with saving a CPT-onomy I’ve assigned to the media attachments post type.

    Can we please bump this for help?

    The problem is that the save_post action doesn’t fired up with the edit attachment page… so we have to add a new action to execute it.

    I’ve fixed it editing cpt-onomies/admin.php file:

    Below line 52:
    add_action( 'save_post', array( &$this, 'save_post' ), 10, 2 );

    Add this:
    add_action( 'edit_attachment', array( &$this, 'save_attachment' ), 10, 2 );

    At the end of the file add this function:

    public function save_attachment($post_id){
    		global $cpt_onomies_manager, $cpt_onomy;
    		// pointless if $_POST is empty (this happens on bulk edit)
    		if ( empty( $_POST ) ){
    			return $post_id;
    		}
    
    		// verify nonce
    		if ( ! ( isset( $_POST[ 'is_bulk_quick_edit' ] ) || ( isset( $_POST[ '_wpnonce' ] ) && wp_verify_nonce( $_POST[ '_wpnonce' ], 'update-' . $post->post_type . '_' . $post_id ) ) || ( isset( $_POST[ CPT_ONOMIES_UNDERSCORE . '_nonce' ] ) && wp_verify_nonce( $_POST[ CPT_ONOMIES_UNDERSCORE . '_nonce' ], 'assigning_' . CPT_ONOMIES_UNDERSCORE . '_taxonomy_relationships' ) ) ) ){
    			return $post_id;
    		}
    
    		// check autosave
    		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
    			return $post_id;
    		}
    
    		// dont save for revisions
    		if ( isset( $post->post_type ) && $post->post_type == 'revision' ){
    			return $post_id;
    		}
    
    		$post = get_post($post_id);
    
    		// check cpt-onomies
    
    		foreach( get_object_taxonomies( $post->post_type, 'objects' ) as $taxonomy => $tax ) {
    
    			// make sure cpt-onomy was visible, otherwise we might be deleting relationships for taxonomies that weren't even "editable"
    			if ( isset( $_POST[ 'assign_cpt_onomies_' . $taxonomy . '_rel' ] ) && $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
    
    				// check permissions
    				if ( ! current_user_can( $tax->cap->assign_terms ) )
    					continue;
    
    				// set object terms
    				if ( isset( $_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ] ) ) {
    
    					// need to make sure its an array
    					if ( ! is_array( $_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ] ) )
    						$_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ] = explode( ',', $_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ] );		
    
    					$cpt_onomy->wp_set_object_terms( $post_id, $_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ], $taxonomy );
    
    				}
    
    				// delete taxonomy relationships
    				else
    					$cpt_onomy->wp_delete_object_term_relationships( $post_id, $taxonomy );
    
    			}
    
    		}
    	}

    This method works for me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Assigning CPT-onomies to Attachments – Not Saving Selection’ is closed to new replies.