Title: Problem with WordPress hook action
Last modified: March 16, 2018

---

# Problem with WordPress hook action

 *  [davideldred](https://wordpress.org/support/users/davideldred/)
 * (@davideldred)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/problem-with-wordpress-hook-action/)
 * I have a artist portfolios website with the following pages.
    Portfolios page–
   parent (summary page all artists) -> Thumbnail page – child page (each 1 for 
   each artist) – > – > Image page – child page (single image for artist x number
   of images). All are custom post type “artist_portfolios”. Each thumbnail page
   has a gallery shortcode (the thumbnails). I have a custom function that runs 
   when saving the thumbnail page which automatically saves (or updates) the required
   number of image pages. (Based on [https://wordpress.stackexchange.com/questions/85827/automatically-create-child-pages-when-saving-a-parent-page](https://wordpress.stackexchange.com/questions/85827/automatically-create-child-pages-when-saving-a-parent-page))
 *     ```
       function arena_add_artist_children($post_id) {  
           if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
       		return;
       	if ('artist_portfolios' == get_post_type( $post_id)
       		&& 'Portfolios' == get_the_title($post->post_parent)
       		&& 'thumbnails' == get_page_template_slug($post_id)
       		&& 'auto-draft' != get_post_status($post_id)) {
       		//---------------------------------------------------------------
       		$counter = 1;
       		//Check if thumbnail page has a gallery
       		if (get_post_gallery()) {
       			//Loop through gallery
       			$gallery = get_post_gallery(get_the_ID(), false);
       			$args = array( 
       				'post_type'      => 'attachment', 
       				'posts_per_page' => -1, 
       				'post_status'    => 'any', 
       				'post__in'       => explode( ',', $gallery['ids'] ) 
       			); 
       			$attachments = get_posts($args);
       			//=============================================================
       			foreach ($attachments as $attachment) {
       				$image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true);
       				//if no alt tag set use post_title or post_excerpt
       				if ( empty( $image_alt )) { $image_alt = $attachment->post_title; }
       				if ( empty( $image_alt )) { $image_alt = $attachment->post_excerpt; }
       				$image_title = $attachment->post_title;
       				$child = array(
       					'post_title'   => $image_title,
       					'post_parent'  => $post_id,
       					'post_content' => 'img source content here',
       					'post_type'    => 'artist_portfolios', 
       					'menu_order'   => $counter
       				);
       				wp_insert_post($child);
       				$counter++;
   
       			}
       		}
       	}
       //---------------------------------------------------------------
       }
       //---------------------------------------------------------------
       add_action('save_post', 'arena_add_artist_children');
       ```
   
 * If I remove these 3 lines
 *     ```
       'Portfolios' == get_the_title($post->post_parent)
       'thumbnails' == get_page_template_slug($post_id)
       'auto-draft' != get_post_status($post_id))<code></code>
       ```
   
 * and change “post_type” to “page” it works. However as I understand it different
   post types can’t have a parent child relationship. If I change the post type 
   to “artist_portfolios” for the image page it results in the function getting 
   in to an endless loop – it saves the first page then creates endless child pages
   of it.
 * So the problem seems to be with the removed lines of code but I can’t see what.
 * Any suggestions?
    -  This topic was modified 8 years, 2 months ago by [davideldred](https://wordpress.org/support/users/davideldred/).
    -  This topic was modified 8 years, 2 months ago by [davideldred](https://wordpress.org/support/users/davideldred/).

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/problem-with-wordpress-hook-action/#post-10085643)
 * The problem is when you call wp_insert_post() from your “save_post” callback,
   your callback is called again as a different instance due to the same “save_post”
   action, which again calls wp_insert_post(), which ends up calling your callback,
   which again…
 * I don’t know why it would work with pages, it shouldn’t. To break the infinite
   loop, before calling wp_insert_post(), remove your callback from the “save_post”
   action stack. Unless your need is recursive, you don’t need your callback added
   back in any more, the rest of the insertion loop will continue.
 * There are no reasons you cannot have posts of different types as parents. Attachments
   are a post type with post or page parents. What you cannot do is have children
   of a different type be listed under their parent in back end list tables. They
   remain children none the less. Your own code would need to make use of this relationship.
   WP wouldn’t make use of it on its own.

Viewing 1 replies (of 1 total)

The topic ‘Problem with WordPress hook action’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/problem-with-wordpress-hook-action/#post-10085643)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
