Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter ramco

    (@ramco)

    Alright!
    This was too obvious actually…

    all I had to do, was to add an append to the wp_set_object_term

    So it’s now:

    //Assign post to child category
    		global $wpdb;
    		if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
    		  return $post_ID;
    		}
    		foreach ($child_cat_name as $child_cat_cat_string) {
    			$child_ccat_id = intval( get_cat_ID( $child_cat_cat_string ) );
    			wp_set_object_terms( $post_ID, $child_ccat_id, 'category', true );
    }

    Last thing:
    I realised, that the new created “child categories” aren’t actually child-categories, that means that they aren’t assigned to a parent upon creation…I don’t see why:

    foreach ($child_cat_name as $child_cat_name_string) {
    			if ( ! category_exists($child_cat_name_string) ) {
    				wp_create_category( $child_cat_name_string, $ccat_id );
    			}
    		}

    Since $ccat_id is made to an integer after the generous help of keesiemeijer, should’nt it work like that? I compared wp_create_category to the codex and this way it should assign the created category to a parent with the given ID, shouldn’t it?

    Thread Starter ramco

    (@ramco)

    I’m a step further now, that means, at least the sub-categories are now created upon post-save.

    I just had to unserialize the data from that album_info_format-Array.
    And had to array_push the according values into my $child_cat_name.

    This part of my code ist now:

    $post_meta_data = get_post_custom($post_ID);
    $get_child_cat_name = unserialize($post_meta_data['album_info_format'][0]);
    $child_cat_name = array();
    foreach ($get_child_cat_name as $child_cat_string) {
    	switch($child_cat_string) {
    		//Cassette
    		case 'mc':
    		array_push($child_cat_name,'Record','Cassette');
    		break;
    
    		//CD
    		case 'cd':
    		array_push($child_cat_name,'Record','CD');
    		break;
    	}
    }
    
    foreach ($child_cat_name as $child_cat_name_string) {
    	if ( category_exists($child_cat_name_string) ) {
    		wp_create_category( $child_cat_name_string, $ccat_id );
    	}
    }

    The code is shortened to just two cases within the switch compared to my original code

    What I am struggling now with is that the post is neither assigned to the parent category (which is “Product” in my case) nor to the specified and created child categories BUT the last one in the array…

    My code for this case hasn’t really changed yet, since I don’t know what to do exactly, where the problem is and everything I tried failed so far.
    The code is:

    //Assign post to child category
    		global $wpdb;
    		if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
    		  return $post_ID;
    		}
    		foreach ($child_cat_name as $child_cat_cat_string) {
    			$child_ccat_id = intval( get_cat_ID( $child_cat_cat_string ) );
    			wp_set_object_terms( $post_ID, $child_ccat_id, 'category' );
    		}
    
    }

    Anyone?

    Thread Starter ramco

    (@ramco)

    Sorry for delay, I had to work the whole day. And thanks for the reply!!

    I changed the code in the save_to_product_category() function as you said.
    Unfortunately this didn’t do the trick…the child category isn’t created nor the post filed under it.

    Actually, I don’t know exactly what you mean here:

    Try setting the $post_meta_data variable as you do in single.php.

    The child-cat. shall be set to the same value like the checkboxes the user clicked in the edit screen under “Format” (i.e. if you check Cassette the function shall file the post under the “Record” and the “Cassette” category, like specified in the array for this case)
    I store these Format values in album_info_format

    I checked if I hand over a wrong variable or a similar mistake, but I can’t see any.
    Do you think the code is too messed up to work with? I really can’t tell, since I am far, far, far from being a php programmer…

    Thread Starter ramco

    (@ramco)

    Sure, my bad, I’m sorry…

    Here is the full code:

    This (https://pastebin.com/ZERUgZ9N)goes in a product-post-type.php that is loaded in the functions.php via require_once('library/product-post-type.php');

    This (https://pastebin.com/ctrAUX9P) is the single-product.php, that will show this “Product”-Custom-Post-Type.

    I’m sorry, this must look terrible. I hope nonetheless it will make things clear.
    Otherwise please let me know, since I still didn’t came up with a solution for this task yet.

    Thanks in advance for any help!!

    Thread Starter ramco

    (@ramco)

    So kessiemeijer’s solution works like a charm! Thanks so much again!

    But now I tried to assign my Custom Post to a child-category depending on the so called “Format” given by a Custom Meta Box value.

    I tried the whole day again to achieve this, searched the forums and web, but I found nothing that really helped me…

    This is what I have so far:

    // Save Product to "Products" category
    function save_to_product_category($post_ID) {
    
    	//PARENT CATEGORY
    	//Create parent category based on Singular Name
    		$get_cat_name = get_post_type_object( 'product' );
    		$cat_name = $get_cat_name->labels->singular_name;
    		if ( ! category_exists($cat_name) ) {
    			wp_create_category( $cat_name );
    		}
    
    	//Assign post to parent category
    		global $wpdb;
    		if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
    		  return $post_ID;
    		}
    		$ccat_id = intval( get_cat_ID( $cat_name ) );
    		wp_set_object_terms( $post_ID, $ccat_id, 'category' );

    This is the part I am searching a solution for

    //CHILD CATEGORY
    	//Create child category based on Format
    		$get_child_cat_name = $post_meta_data['album_info_format'][0];
    		foreach ($get_child_cat_name as $child_cat_string) {
    																    switch($child_cat_string) {
    		//Cassette
    			case 'mc':
    			$child_cat_name = array('Record','Cassette');
    			break;
    		//CD
    			case 'cd':
    			$child_cat_name = array('Record','CD');
    			break;
    		//Vinyl
    			case 'lp':
    			$child_cat_name = array('Record','Vinyl');
    			break;
    		//Digital
    			case 'mp':
    			$child_cat_name = array('Record','Digital');
    			break;
    		//Magazine
    			case 'zine':
    			$child_cat_name = array('Print','Magazine');
    			break;
    		//Print
    			case 'print':
    			$child_cat_name = array('Print');
    			break;
    		//Video
    			case 'video':
    			$child_cat_name = array('Video');
    			break;
    		//Apparel
    			case 'apparel':
    			$child_cat_name = array('Apparel');
    			break;
    		//Textile
    			case 'textile':
    			$child_cat_name = array('Textile');
    			break;
    		//Merch
    			case 'merch':
    			$child_cat_name = array('Merchandise');
    			break;
    		//Other
    			case 'other':
    			$child_cat_name = array('Other Formats');
    			break;
    }
    															}
    		foreach ($child_cat_name as $child_cat_name_string) {
    			if ( ! category_exists($child_cat_name_string) ) {
    				wp_create_category( $child_cat_name_string, $ccat_id );
    			}
    		}
    
    	//Assign post to child category
    		global $wpdb;
    		if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
    		  return $post_ID;
    		}
    		foreach ($child_cat_name as $child_cat_cat_string) {
    			$child_ccat_id = intval( get_cat_ID( $child_cat_cat_string ) );
    			wp_set_object_terms( $post_ID, $child_ccat_id, 'category' );
    		}
    
    }
    
    add_action('publish_product', 'save_to_product_category');

    The idea is almost the same as it is for the parent category:

    1. Get the “Format” and relate the Categories to that format
    2. Check if there is already such a cat. If not create one under the specified Parent (here: Product)
    3. Assign Custom Post to the Categories

    I hope my code is not such a disaster as it feels like after working on it the whole day without any result…

    Thread Starter ramco

    (@ramco)

    Oh my god! You are a genius!
    I thank you so much!!
    Seems as if it works as it should!! Thank you!

    Thread Starter ramco

    (@ramco)

    No one? I’m still trying, but totally messed it up…so the code above is still my “most recent” ??

Viewing 7 replies - 1 through 7 (of 7 total)