• Hi,

    I’ve got a custom post type ‘news’ as well as a custom taxonomy ‘news-cat’ (category)

    The only reason I called it news-cat (and not ‘news’) as it’d clash with ‘news’ post type. At the moment the urls are as follows:

    All posts of type ‘news’: domain.com/news/
    A single post of type ‘news’: domain.com/news/my-news-article

    We have a number of terms within the news-cat taxonomy (eg. family, trips, etc.)
    If I query a certain term to get all news of certain term: eg. family, the url is domain.com/news-cat/family/
    Is it possible to rewrite it somehow so that wordpress won’t confuse a taxonomy term and a single news item. Ideally, we’d like to have:
    domain.com/news/family/ for a taxonomy term query
    domain.com/news/single-news-article for a single post of a custom type ‘news’

    function news_taxonomy() {
    $labels = array(
    'name'              => _x( 'News Category', 'Taxonomy General Name' ),
        'singular_name'     => _x( 'News Category', 'Taxonomy Singular Name' ),
        'search_items'      => __( 'Search News Category' ),
        'all_items'         => __( 'All News Categories' ),
        'parent_item'       => __( 'Parent News Category' ),
        'parent_item_colon' => __( 'Parent News Category:' ),
        'edit_item'         => __( 'Edit News Category' ),
        'update_item'       => __( 'Update News Category' ),
        'add_new_item'      => __( 'Add News Category' ),
        'new_item_name'     => __( 'New News Category' ),
        'menu_name'         => __( 'News Category' ),
      );
    
      $args = array(
        'hierarchical'      => true,     // like categories or tags
        'labels'            => $labels,
        'show_in_rest'      => true,
        'show_ui'           => true,     //  add the default UI to this taxonomy
        'show_admin_column'=> true,    // add the taxonomies to the wordpress admin
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'news-cat'),
      );
    
      register_taxonomy( 'news_tax', 'news', $args );
    }
    
    register_post_type('news', array(
                'has_archive' => true,
                'rewrite' => array('slug' => 'news'),
                'supports' => array('title', 'editor', 'excerpt', 'thumbnail'),
                'public' => true,
                'show_in_rest' => true,
                'labels' => array(
                    'name' => 'News',
                    'add_new_item' => 'Add News',
                    'edit_item' => 'Edit News',
                    'all_items' => 'All News',
                    'singular_name' => 'News'
                ),
                'menu_icon' => 'dashicons-calendar-alt'
            ));

    Please advise.

Viewing 4 replies - 1 through 4 (of 4 total)
  • They have to be distinct to show different things.
    What if instead of news-cat you used topics? or gossip? or some other synonym?

    Thread Starter portia79

    (@portia79)

    Thanks for your reply. Yeah, ideally, I’d want to avoid using another term. I was thinking that perhaps there is some way of doing a URL/slug rewrite so that for wordpress engine they are distinct (news and news-cat) but for the viewer seeing the url in a browser they all have /news/ in the url. Is that at all possible?

    WordPress sees the same URL as the visitor, and has to figure out what to retrieve based solely on that.

    Thread Starter portia79

    (@portia79)

    OK. Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘url rewrite for custom taxonomy’ is closed to new replies.