Wednesday 15 February 2012

php - WordPress custom post type cannot access child page -



php - WordPress custom post type cannot access child page -

the below allows me utilize programmes , programme faq in wordpress website custom post types i'm struggling permalinks of pages.

when create new faq page set parent programme fine, permalink cannot seem working.

if illustration programme called football visit domain.com/programmes/footballthen create programme faq same name football , set parent football game programme , permalink faq become domain.com/programme_faq/football/football

when seek visit page comes 404 not found. if remove parent alternative faq page permalink becomes domain.com/programme_faq/football , works.

ideally prefer pages made in programme faq end domain.com/programmes/football/faq

add_action( 'init', 'register_cpt_programmes' ); add_action( 'init', 'register_cpt_programmes_faq' ); function register_cpt_programmes() { $labels = array( 'name' => __( 'programmes', 'text_domain' ), 'singular_name' => __( 'single programme name', 'text_domain' ), 'add_new' => _x( 'add programme', '${4:name}', 'text_domain' ), 'add_new_item' => __( 'add programme', 'text_domain}' ), 'edit_item' => __( 'edit programme', 'text_domain' ), 'new_item' => __( 'new programme', 'text_domain' ), 'view_item' => __( 'view programme', 'text_domain' ), 'search_items' => __( 'search programmes', 'text_domain' ), 'not_found' => __( 'no programmes found', 'text_domain' ), 'not_found_in_trash' => __( 'no programmes found in trash', 'text_domain' ), 'parent_item_colon' => __( 'parent single post type name:', 'text_domain' ), 'menu_name' => __( 'programmes', 'text_domain' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'description' => 'description', //'taxonomies' => array( 'category' ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, //'menu_icon' => '', 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'page', 'supports' => array( 'title', 'editor', 'author', 'page-attributes', 'thumbnail', 'excerpt', 'custom-fields', 'revisions', 'comments' ), 'rewrite' => array( 'with_front' => false, 'slug' => 'programmes' ) ); register_post_type( 'programmes', $args ); } function register_cpt_programmes_faq() { $labels = array( 'name' => __( 'programme faq', 'text_domain' ), 'singular_name' => __( 'single programme faq', 'text_domain' ), 'add_new' => _x( 'add programme faq', '${4:name}', 'text_domain' ), 'add_new_item' => __( 'add programme faq', 'text_domain}' ), 'edit_item' => __( 'edit programme faq', 'text_domain' ), 'new_item' => __( 'new programme faq', 'text_domain' ), 'view_item' => __( 'view programme faq', 'text_domain' ), 'search_items' => __( 'search programme faq', 'text_domain' ), 'not_found' => __( 'no programme faqs found', 'text_domain' ), 'not_found_in_trash' => __( 'no programmes faqs found in trash', 'text_domain' ), 'parent_item_colon' => __( 'parent single post type name:', 'text_domain' ), 'menu_name' => __( 'programme faq', 'text_domain' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'description' => 'description', //'taxonomies' => array( 'category' ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, //'menu_icon' => '', 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'page', 'supports' => array( 'title', 'editor', 'author', 'page-attributes', 'thumbnail', 'excerpt', 'custom-fields', 'revisions', 'comments' ), 'rewrite' => array( 'with_front' => false, 'slug' => 'programme_faq' ) ); register_post_type( 'programme_faq', $args ); } add_action('admin_menu', function() { remove_meta_box('pageparentdiv', 'programme_faq', 'normal'); }); add_action('add_meta_boxes', function() { add_meta_box('programmes-parent', 'programmes', 'programmes_attributes_meta_box', 'programme_faq', 'side', 'high'); }); function programmes_attributes_meta_box($post) { $post_type_object = get_post_type_object($post->post_type); if ( $post_type_object->hierarchical ) { $pages = wp_dropdown_pages(array('post_type' => 'programmes', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0)); if ( ! empty($pages) ) { echo $pages; } // end empty pages check } // end hierarchical check. }

this first time answering on stack overflow. please pardon formatting , spelling issues.

summary:

the key steps in reaching solution documented in code. here summary:

register custom post types add "parent programme" meta box on edit screen add rewrite rule wordpress parses custom url structure filter permalink links faq pages rendered properly

code:

// 1a. register programmes post type function register_cpt_programmes() { $labels = array( 'name' => __('programmes', 'text_domain'), 'singular_name' => __('single programme name', 'text_domain'), 'add_new' => _x('add programme', '${4:name}', 'text_domain'), 'add_new_item' => __('add programme', 'text_domain}'), 'edit_item' => __('edit programme', 'text_domain'), 'new_item' => __('new programme', 'text_domain'), 'view_item' => __('view programme', 'text_domain'), 'search_items' => __('search programmes', 'text_domain'), 'not_found' => __('no programmes found', 'text_domain'), 'not_found_in_trash' => __('no programmes found in trash', 'text_domain'), 'parent_item_colon' => __('parent single post type name:', 'text_domain'), 'menu_name' => __('programmes', 'text_domain'), ); $args = array( 'labels' => $labels, 'hierarchical' => false, 'description' => 'description', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'page', 'supports' => array( 'title', 'editor', 'author', 'page-attributes', 'thumbnail', 'excerpt', 'custom-fields', 'revisions', 'comments' ), 'rewrite' => array( 'with_front' => false, 'slug' => 'programmes' ) ); register_post_type('programmes', $args); } add_action('init', 'register_cpt_programmes'); // 1b. register programme faq post type function register_cpt_programme_faq() { $labels = array( 'name' => __('programme faq', 'text_domain'), 'singular_name' => __('single programme faq', 'text_domain'), 'add_new' => _x('add programme faq', '${4:name}', 'text_domain'), 'add_new_item' => __('add programme faq', 'text_domain}'), 'edit_item' => __('edit programme faq', 'text_domain'), 'new_item' => __('new programme faq', 'text_domain'), 'view_item' => __('view programme faq', 'text_domain'), 'search_items' => __('search programme faq', 'text_domain'), 'not_found' => __('no programme faqs found', 'text_domain'), 'not_found_in_trash' => __('no programmes faqs found in trash', 'text_domain'), 'parent_item_colon' => __('parent single post type name:', 'text_domain'), 'menu_name' => __('programme faq', 'text_domain'), ); $args = array( 'labels' => $labels, 'hierarchical' => false, 'description' => 'description', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'page', 'supports' => array( 'title', 'editor', 'author', 'page-attributes', 'thumbnail', 'excerpt', 'custom-fields', 'revisions', 'comments' ), 'rewrite' => array( 'with_front' => false, 'slug' => 'programme_faq' ) ); register_post_type('programme_faq', $args); } add_action('init', 'register_cpt_programme_faq'); // 2a. meta box - add together custom meta box on programme faq edit page function pfaq_add_meta_boxes($post) { add_meta_box('pfaq-parent', 'parent programme', 'pfaq_parent_meta_box', $post->post_type, 'side', 'core'); } add_action('add_meta_boxes_programme_faq', 'pfaq_add_meta_boxes'); // 2b. select box - add together select input within parent programme meta box function pfaq_parent_meta_box($post) { $parents = get_posts( array( 'post_type' => 'programmes', 'orderby' => 'title', 'order' => 'asc', 'numberposts' => -1 ) ); if(!empty($parents)) { echo '<select name="parent_id" class="widefat">'; foreach($parents $parent) { printf('<option value="%s"%s>%s</option>', esc_attr($parent->id), selected($parent->id, $post->post_parent, false), esc_html($parent->post_title)); } echo '</select>'; } } // 3. rewrite rule - teach wordpress parse custom url pattern function pfaq_rewrite_rule() { add_rewrite_rule('^programmes/([^/]+)/faq/?', 'index.php?programme_faq=$matches[1]-faq', 'top'); } add_action('init', 'pfaq_rewrite_rule'); // 4. permalink - filter programme faq permalinks match desired url pattern function pfaq_post_type_link($post_link, $post, $leavename, $sample) { if($post->post_type == 'programme_faq') { $parent = get_post($post->post_parent); $post_link = get_bloginfo('url') . '/programmes/' . $parent->post_name . '/faq'; } homecoming $post_link; } add_filter('post_type_link', 'pfaq_post_type_link', 1, 4);

notes:

i've repeated code original question reflect couple of of import changes:

the solution requires non-hierarchical post types renamed functions consistency corrected issues in meta box code corrected "programme" vs. "programmes" functionality impacted

the prefix "pfaq" chosen of customizations revolve around "programme faq" post type.

credits:

wordpress core, wordpress codex, @justintadlock, @tareq_cse

php wordpress rewrite

No comments:

Post a Comment