0

This is my first post, so maybe I'll miss some informations, sorry :bowing.

I have almost the same issue as Paul T did (Permalinks: custom post type -> custom taxonomy -> post), namely :

I have a custom post type called sioban_cpt, which have a custom taxonomy sioban_taxo.

A sioban_cpt can have more than one sioban_taxo.

I would like my urls to route in this manner:

www.mysite.com/sioban_cpt/  =>  archive-sioban-cpt.php
www.mysite.com/sioban_cpt/%sioban_taxo%/ => taxonomy-sioban_taxo.php
**www.mysite.com/sioban_cpt/%postname%/ => single-sioban_cpt.php**

So :

www.mysite.com/sioban_cpt/ => shows all posts with sioban_cpt post-type (this works)
www.mysite.com/sioban_cpt/foo/ => shows all posts with the foo taxonomy(this works)
www.mysite.com/sioban_cpt/bar => shows the indivual post under the sioban_cpt post-type (this doesn't work, it display the error template, although www.mysite.com/sioban_cpt/bar is the shown permalink in the WP admin post edition page)

As Paul T, I try to avoid plugins.

I don't know if a solution is possible, because I want the taxonomy and the posts of the CPT both as descendants of the cpt, and conversely I don't want the posts of the CPT as children of the taxo.

Here is my code in function.php :

/**
 * create new custom taxonomy (has to be called before CPT declaration)
 *
 * @return void
 */
function si0b_ct_year()
{
    /* Property Type */
    $labels = array(
       'name'                       => _x('Years', 'Taxonomy General Name', 'siobanone'),
       'singular_name'              => _x('Year', 'Taxonomy Singular Name', 'siobanone'),
       'menu_name'                  => __('Année de participation', 'siobanone'),
       'all_items'                  => __('Toutes les années', 'siobanone'),
       'new_item_name'              => __('Nouvelle année', 'siobanone'),
       'add_new_item'               => __('Ajouter une nouvelle année', 'siobanone'),
       'edit_item'                  => __('Modifier l\'année', 'siobanone'),
       'update_item'                => __('Actualiser l\année', 'siobanone'),
       'view_item'                  => __('Voir l\année', 'siobanone'),
       'separate_items_with_commas' => __('Separate years with commas', 'siobanone'),
       'add_or_remove_items'        => __('Add or remove years', 'siobanone'),
       'search_items'               => __('Search Years', 'siobanone'),
       'not_found'                  => __('Not Found', 'siobanone'),
       'no_terms'                   => __('No years', 'siobanone'),
       'items_list'                 => __('Years list', 'siobanone'),
       'items_list_navigation'      => __('Years list navigation', 'siobanone'),
   );
    $rewrite = array(
       'slug'                => __('aec-year', 'siobanone'),
       'with_front'          => false
   );
    $args = array(
       'labels'                     => $labels,
       'public'                     => true,
       'show_ui'                    => true,
       'show_admin_column'          => true,
       'show_in_nav_menus'          => true,
       'show_in_quick_edit'         => true,
       'rewrite'                    => $rewrite,
       'show_in_rest'               => true,
       'has_archive'                => true,
   );
    register_taxonomy('aec-year', 'artists', $args);
}

add_action('init', 'si0b_ct_year', 10);

/*To create new custom post type */
function si0b_cpt_artists()
{
    // CPT : artists

    $labels = array(
       'name'                => _x('Artists', 'Post Type General Name', 'siobanone'),
       'singular_name'       => _x('Artist', 'Post Type Singular Name', 'siobanone'),
       'menu_name'           => __('Artists', 'siobanone'),
       'name_admin_bar'      => __('Artists', 'siobanone'),
       'parent_item_colon'   => __('Parent Item:', 'siobanone'),
       'all_items'           => __('All artists', 'siobanone'),
       'add_new_item'        => __('Ajouter nouvel.le artiste', 'siobanone'),
       'add_new'             => __('Ajouter', 'siobanone'),
       'new_item'            => __('New Item', 'siobanone'),
       'edit_item'           => __('Edit Item', 'siobanone'),
       'update_item'         => __('Update Item', 'siobanone'),
       'view_item'           => __('View Item', 'siobanone'),
       'search_items'        => __('Search Item', 'siobanone'),
       'not_found'           => __('Not found', 'siobanone'),
       'not_found_in_trash'  => __('Not found in Trash', 'siobanone'),
   );
    $rewrite = array(
       'slug'                => _x('artistes/%aec-year%', 'siobanone'),
       'with_front'          => false
   );
    $args = array(
       'label'               => __('artists', 'siobanone'),
       'description'         => _x('Artists', 'siobanone'),
       'labels'              => $labels,
       'supports'            => array('title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'excerpt'),
       'taxonomies'          => array('aec-year'),
       'hierarchical'        => false,
       'public'              => true,
       'show_ui'             => true,
       'show_in_menu'        => true,
       'menu_position'       => 5,
       'menu_icon'           => 'dashicons-images-alt',
       'show_in_admin_bar'   => true,
       'show_in_nav_menus'   => true,
       'can_export'          => true,
       'has_archive'         => 'artistes',
       'exclude_from_search' => false,
       'rewrite'             => $rewrite,
   );
    register_post_type('artists', $args);
}

add_action('init', 'si0b_cpt_artists', 10);

/**
 *  add a filter to post_type_link to substitute the taxo aec-year in individual CPT artists permalinks
 *
 * @param [type] $post_link
 * @param [type] $post
 * @return void
 */
function si0b_artists_permalinks($post_link, $post)
{
    if (is_object($post) && $post->post_type == 'artists') {
        $terms = wp_get_object_terms($post->ID, 'aec-year');
        if ($terms) {
            return str_replace('%aec-year%', $terms[0]->slug, $post_link);
        }
    }
    return $post_link;
}
add_filter('post_type_link', 'si0b_artists_permalinks', 1, 2);

Thanks for your help...

6
  • Have a look at this question (and the answer linked from that question).
    – Sally CJ
    Commented Mar 5, 2020 at 0:32
  • Thanks a lot Sally ! I can't test the code before next week, but it seems to be the solution 🤩 (Haha, I'm so excited to test it).
    – Sioban
    Commented Mar 6, 2020 at 6:27
  • I've actually updated that answer, so check it out, if you haven't already. :)
    – Sally CJ
    Commented Mar 7, 2020 at 12:06
  • 1
    Sally, I tried your solution and it works like a hot damn ! You're a genius ! Thanks a lot ! I just let the 'has_archive' property with the original CPT name and no with the custom slug to allow my searchbar in the CPT archive template to work. Thanks *****
    – Sioban
    Commented Mar 13, 2020 at 21:16
  • Hm I didn't find how to mark the topic as resolved ^ ^' ..
    – Sioban
    Commented Mar 13, 2020 at 21:23

1 Answer 1

0

Thanks to Sally CJ, here is the solution that worked for me :

/*To create new custom taxonomy  */
function si0b_ct_year()
{
    /* Property Type */
    $labels = array(
        'name'                       => _x('Years', 'Taxonomy General Name', 'siobanone'),
        'singular_name'              => _x('Year', 'Taxonomy Singular Name', 'siobanone')
    );

    $rewrite = array(
        'slug'                     => 'artistes',
        'with_front'   => false,
        'hierarchical' => true,
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'rewrite'                    => $rewrite,
        'show_in_rest'               => true,
    );
    register_taxonomy('aec-year', array('artists'), $args);
}

add_action('init', 'si0b_ct_year', 10);

/*To create new custom post type */
function si0b_cpt_artists()
{
    // CPT : artists

    $labels = array(
        'name'                => _x('Artists', 'Post Type General Name', 'siobanone'),
        'singular_name'       => _x('Artist', 'Post Type Singular Name', 'siobanone'),
    );

    $rewrite = array(
        'slug'                => 'artistes',
        'with_front'          => false,
    );
    $args = array(
        'label'               => __('artists', 'siobanone'),
        'description'         => __('Artists', 'siobanone'),
        'labels'              => $labels,
        'taxonomies'          => array('aec-year', 'post_tag'),
        'hierarchical'        => true,
        'public'              => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'query_var'           => 'artists',
        'rewrite'             => $rewrite,
        'show_in_rest'               => true,
    );
    register_post_type('artists', $args);
}

add_action('init', 'si0b_cpt_artists', 10);

/**Allow this :
 * www.mysite.com/sioban_cpt/foo/ => shows all posts with the foo taxonomy
 * www.mysite.com/sioban_cpt/bar => shows the indivual post under the sioban_cpt post-type
**/
function si0b_parse_request( $wp ) {
    $path      = 'artistes'; // rewrite slug; no trailing slashes
    $taxonomy  = 'aec-year'; // taxonomy slug
    $post_type = 'artists';  // post type slug

    if ( preg_match( '#^' . preg_quote( $path, '#' ) . '/#', $wp->request ) &&
        isset( $wp->query_vars[ $taxonomy ] ) ) {
        $slug = $wp->query_vars[ $taxonomy ];
        $slug = ltrim( substr( $slug, strrpos( $slug, '/' ) ), '/' );

        if ( ! term_exists( $slug, $taxonomy ) ) {
            $wp->query_vars['name']       = $wp->query_vars[ $taxonomy ];
            $wp->query_vars['post_type']  = $post_type;
            $wp->query_vars[ $post_type ] = $wp->query_vars[ $taxonomy ];
            unset( $wp->query_vars[ $taxonomy ] );
        }
    }
}
add_action( 'parse_request', 'si0b_parse_request' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.