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)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?
The topic ‘Problem with WordPress hook action’ is closed to new replies.