PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Add Parent post category with hook/filter

  1. sascha
    Member

    Hi there,
    I am using the "post category" field to populate the category of a post. This works well. What I would like to do though is:
    if a child is chosen on the form, then the parent (or even parents) are added automatically with the help of a hook/filter. Which would be the best filter to use and how would I go about adding that parent to the post category?
    In logic I would:
    1. get the field value of the post category field into $child_category
    2. use get_category_parent ($child_category) to save parent in $parent
    3. wp_set_post_terms ($post_ID, $parent, $category, true)

    Do not know if that is right though. If it is the questions I have: what do I use for $post_ID and what do I use for $category?
    As always, any help is appreciated!

    Posted 12 years ago on Monday November 28, 2011 | Permalink
  2. Hi Sascha,

    This is probably easiest to accomplish after the post has been created using the gform_after_submission hook as I did to do a related task in this topic:

    http://www.gravityhelp.com/forums/topic/how-to-create-a-form-that-posts-a-child-post-or-child-page

    You will need to fetch the parent category ID by passing the selected child category (available in the $entry object) to the WP function get_category(). This function will return a category object which includes the "parent" property. This will be the ID of the parent category.

    Per my code example above, the $post_id is available in the $entry object as well.

    Posted 12 years ago on Monday November 28, 2011 | Permalink
  3. sascha
    Member

    Hi Dave,
    I used the following code and it works great! Thanks for your help/pointers!!!

    // select parent term automatically for the 'project_type' taxonomy in form #1
    add_action('gform_after_submission_1', 'gform_parent_category');
    function gform_parent_category($entry) {
    
    	$created_post = $entry['post_id'];
    	$child_ID = $entry["7"];
    	$term_object = get_term_by('id', $child_ID, 'project_type'); // replace project_type by chosen taxonomy
    	$parent_ID = $term_object -> parent;
    	wp_set_post_terms( $created_post, $parent_ID, 'project_type', true ); //replace project_type by chosen taxonomy
    }

    But instead of copying the code for each form, I wanted a flexible solution: I tried to use wp_get_post_terms instead. The only problem is that function returns an empty string! No idea why?!? Quite messy code as I tried different options:

    add_action('gform_after_submission', 'gform_parent_category');
    function gform_parent_category($entry) {
    
    	$created_post = $entry['post_id'];
    	$post = get_post($entry["post_id"]);
    	//$child_ID = $entry["7"];
    	//$term_object = get_term_by('id', $child_ID, 'project_type'); // replace project_type by chosen taxonomy
    	$query_taxonomy = 'project_type';
    	$term_object = wp_get_post_terms($created_post, $query_taxonomy, array("fields" => "all")); // replace project_type by chosen taxonomy
    	$product_terms = wp_get_object_terms($created_post, 'project_type', array("fields" => "all"));
    	$parent_ID = $term_object -> parent;
    	print_r($term_object);
    	var_dump($term_object);
    	var_dump($product_terms);
    	var_dump($post);
    	//$parent_ID = $term_object[parent];
    	echo $parent_ID;
    	wp_set_post_terms( $created_post, $parent_ID, 'project_type', true ); //replace project_type by chosen taxonomy
    }

    Have you got any clues?

    Posted 12 years ago on Saturday December 3, 2011 | Permalink
  4. I'm a PHP rookie so let me know if I'm out to lunch.

    I want to do something similar to Sascha and have implemented this function that automatically sets the ancestor taxonomy term with the descendent is selected.

    It works great when publishing/editing a post from the backend - but not when a post is created with gravity forms.

    Any ideas on how this function can be hooked in with 'gform_after_submission'?
    Also trying to make this work with front-end editor but don't know how to hook it in.

    http://pastebin.com/raw.php?i=P3fACKYq

    Posted 11 years ago on Thursday May 24, 2012 | Permalink
  5. This behavior can be achieved globally with a wordpress hook. There is no need for the 'gform_after_submission' hook.

    Copy this: http://pastebin.com/tcvfg9L9

    If you're using default posts try hooking to the action 'save_post', 'publish_post' or 'edit_post', etc. If you're using a custom post type, instead do 'save_my-post-type', where my-post-type is your post type's slug.

    Posted 11 years ago on Friday May 25, 2012 | Permalink