Skip Navigation

[Resolved] I need to populate child taxonomy from parent's taxonomy

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by peterv-6 6 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#521382

I am trying to populate a child taxonomy from a parent taxonomy.

I previously got help from Minesh to populate a child taxonomy from a parent custom field. here: https://toolset.com/forums/topic/converting-data-from-a-checkbox-field-of-a-parent-cpt-to-populate-child-taxonomy/

While trying to get a MySQl request written to generate a table from my data I discovered that the way I was storing data in the Relevant field was causing problems... I was using multi checkboxes which stores the data in serialised form... which apparently doesn't work for mySQL.

The best solution to this problem is to change the way we are storing the State field for companies from Multi-checkboxes to a taxonomy. I have created a taxonomy for the parent "company-state" and want to populate the child taxonomy "Announcement-state". The taxonomy "company-state" is replacing the custom field "state" of "company-profile".

I tried unsuccessfully to revise the code that Minesh had written. see below. Not sure what I am doing wrong.... probably linked to the change from Custom field of parent to a taxonomy.

* Custom code to populate taxonomy - Announcement State
* to enable State by state rss feeds
*/
if ( $form_data['id'] == 1564) {
$parent_id = $_GET['parent_company-profile_id'];

// armed with parent id, get the state field of parent
$parent_state = get_post_meta( $parent_id, 'wpcf-company-state', true );

$parent_state_values = array_values($parent_state);

// array that binds parent state value with child taxonomy slug
$terms = array('western-australia'=>'western-australia','south-australia'=>'south-australia','victoria'=>'victoria','new-south-wales'=>'new-south-wales','queensland'=>'queensland','tasmania'=>'tasmania','northern-territory'=>'northern-territory',
'australian-external-territories'=>'australian-external-territories','overseas'=>'overseas','australian-capital-territory'=>'australian-capital-territory');

$set_terms = array();
foreach($parent_state_values as $k=>$v):
$term = get_term_by('slug',$terms[$v[0]] , 'announcement-state');
$set_terms[] = $term->term_id;
endforeach;
wp_set_object_terms($post_id, $set_terms, 'announcement-state');

}

Can you please help.

#521411

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please tell me whats not working now and problem URL where I can see the issue or steps to reproduce the issue.

I would like to have one test case example and your expected results, few screenshot may help to understand your issue.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#522076

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

OK - I see the new taxonomy company-state and its attached to CPT company profiels.

Now - when I try to click on button "Make announcement" I do not able to see the CRED form - Do I require to login with different access details - could you please advise?

#522088

Minesh

I just logged in with the credentials I gave you... I got to the CRED form without problems... it took a little while to load for some reason... but I got through to the form.

Could you please try again?

Cheers

Peter

#522114

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I tried with the different browser and still no luck. I get same result - form is not loaded on the following link:
=> hidden link

#522190

Minesh

I tried it again logged in with the same credentials... and it worked... it was slow to load.... first you get a notice that you need to be logged in and then the form appears.

Please try again... maybe your connection speed is slower than mine at present.

Thanks.

Peter

#522549

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Finally - i'm able to see the CRED form.

Could you please clarify if I make announcement what taxonomy should be assigned to which post? If you provide one test case example it will really help me to guide you with your desired solution.

4. go to the "my Profiles" page on my site (<em><u>hidden link</u></em>) that enables me to see a button "make an announcement" that is connected to my company.
5. click on the "make an announcement" button to create a test announcement for Techboard using CRED form 1564
5. go to the Announcements main page (<em><u>hidden link</u></em>) open the most recent announcement and then select "edit post" from the top WP menu bar to see if the states selected have populated the "Announcement-state" custom taxonomy for the test announcement (on the RH margin)... it had not.

I have set the next reply to private which means only you and I have access to it.

#522624

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've adjusted to code as per your requirement and comment out the unwanted code and I've tested with a couple of entries and now parent terms automatically assigned to child post.


/**
 * Custom code to force add thumbnail meta
 * to add announcement CRED form post
 */
function force_thumbnail( $post_id, $form_data ) {
	
	
	/**
 * Custom code to populate taxonomy - Announcement State
 * to enable State by state rss feeds
 */
if ( $form_data['id'] == 1564) {
 $parent_id = $_GET['parent_company-profile_id'];
  
    // armed with parent id, get the state field of parent
    //$parent_state = get_post_meta( $parent_id, 'company-state', true );
     $parent_terms = wp_get_post_terms( $parent_id , 'company-state',  array("fields" => "all") );

	
  //$parent_state_values = array_values($parent_state);
 
    // array that binds parent state value with child taxonomy slug
 
/*   
$terms = array('western-australia'=>'western-australia','south-australia'=>'south-australia','victoria'=>'victoria','new-south-wales'=>'new-south-wales','queensland'=>'queensland','tasmania'=>'tasmania','northern-territory'=>'northern-territory',
   'australian-external-territories'=>'australian-external-territories','overseas'=>'overseas','australian-capital-territory'=>'australian-capital-territory'); 
 
        $set_terms = array();
        foreach($parent_state_values as $k=>$v):
            $term = get_term_by('slug',$terms[$v[0]] , 'announcement-state');
            $set_terms[] = $term->term_id;
        endforeach;
*/

        $set_terms = array();
        foreach($parent_terms as $k=>$v):
               $set_terms[] = $v->slug;
        endforeach;

        wp_set_object_terms($post_id, $set_terms, 'announcement-state');
  
	}

	
	
 
    if ( $form_data['id'] == 1564 && !empty( $_POST['_featured_image'] ) ) {
 
        $args = array(
            'numberposts'   => 1,
            'post_type'     => 'attachment',
            'post_parent'   => $post_id
        );
        $thumb = get_posts( $args );
 
        $meta_update = add_post_meta( $post_id, "_thumbnail_id", $thumb[0]->ID, true );
 
    }
}
add_action( 'cred_submit_complete', 'force_thumbnail', 10, 3 );
#522640

Awesome Minesh

I will do some testing before I close this off.

Thanks so much

Peter

#522642

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It should work. Waiting for your confirmation 🙂

#522847

Awesome... after extensive testing... all sorted.

Thanks so much Minesh

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.