rewrite endpoint combined with custom permastruct
-
I have an issue with a new endpoint i am creating.
i have a custom posttype ‘products’ and a custom taxonomy ‘prodcategory’
For the products i created a custom permastruct which looks like:
/products/%prodcategory%/%product%/$products_structure = '/products/%prodcategory%/%mline_products%'; add_rewrite_tag("%prodcategory%", '([^/]+)', "prodcategory="); add_rewrite_tag("%mline_products%", '([^/]+)', "mline_products="); add_permastruct('mline_products', $products_structure, false);
i also added a filter to the post_type_link filter to create the permalinks.
// Adapted from get_permalink function in wp-includes/link-template.php function mline_cpt_products_permalink($permalink, $post, $leavename) { //global $wp_query; $rewritecode = array( '%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', $leavename? '' : '%postname%', '%post_id%', '%category%', '%prodcategory%', '%author%', $leavename? '' : '%pagename%' ); if ( empty($post->ID) ) return false; if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) { $unixtime = strtotime($post->post_date); $category = ''; if ( strpos($permalink, '%category%') !== false ) { $cats = get_the_category($post->ID); if ( $cats ) { usort($cats, '_usort_terms_by_ID'); // order by ID $category = $cats[0]->slug; if ( $parent = $cats[0]->parent ) $category = get_category_parents($parent, false, '/', true) . $category; } // show default category in permalinks, without // having to assign it explicitly if ( empty($category) ) { $default_category = get_category( get_option( 'default_category' ) ); $category = is_wp_error( $default_category ) ? '' : $default_category->slug; } } $prodCategory = ''; if ( strpos($permalink, '%prodcategory%') !== false ) { // get the genre: $terms = get_the_terms($post->ID, 'prodcategory'); if( is_wp_error($terms) || !$terms ) { $prodCategory = 'uncategorised'; } else { $prodCategory_obj = array_pop($terms); $prodCategory = $prodCategory_obj->slug; } } $author = ''; if ( strpos($permalink, '%author%') !== false ) { $authordata = get_userdata($post->post_author); $author = $authordata->user_nicename; } $date = explode(" ",date('Y m d H i s', $unixtime)); $rewritereplace = array( $date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->post_name, $post->ID, $category, $prodCategory, $author, $post->post_name ); $permalink = str_replace($rewritecode, $rewritereplace, $permalink); } else { // if they're not using the fancy permalink option } return $permalink; } add_filter('post_type_link', 'mline_cpt_products_permalink', 10, 3);
This all works fine, my product-slugs look like i.e. /products/pillows/pillow-1
now, i want to create a new endpoint /compare to compare a specific product. (i’m aware that i can use a different ep-mask, but for testing i’m using EP_ALL so i can test it on pages without a custom permastruct.
add_rewrite_endpoint( 'compare', EP_ALL); function compare_template_redirect() { global $wp_query; if (!isset( $wp_query->query_vars['compare'])) : return; endif; if (have_posts()) : the_post(); endif; include get_template_directory() . '/template_compare.php'; exit; } add_action( 'template_redirect', 'compare_template_redirect' );
now, when i go to a page with this endpoint, i.e. /contact/compare
i get the correct template-file (template_compare.php) and i also get the correct data. But when i go to /products/pillows/pillow-1/compare i DO get the correct template-file, but i don’t get the custom-post-type data (have_posts() returns false).Anyone has an idea how this happens? When i do a var_dump of $wp_query i can see that on my custom-permastruct page it tries to get an attachment and no post_type is set.
I can’t figure out what i’m doing wrong here. Perhaps it has something to do with the filter? Also, when i comment out the add_permatruct and add_rewrite_tag, the compare endpoint works just fine aswell on this custom-post-type
- The topic ‘rewrite endpoint combined with custom permastruct’ is closed to new replies.