最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Allow duplicate slugs for custom post type with taxonomies

matteradmin7PV0评论

I have set up a custom post type (team) and a custom taxonomy (team_tax) for this post type, both with the same slug. Down to the core:

register_post_type( 'team', array(
    'labels'       => array( 'Name' => 'Pages' ),
    'hierarchical' => false,
    'rewrite'      => array( 'slug' => 'team', 'with_front' => true ),
    'has_archive'  => 'teams'
  );
);

register_taxonomy( 'team_tax', 'team', array(
    'hierarchical' => true,
    'rewrite'      => array( 'slug' => 'team', 'with_front' => true )
  );
);

The taxonomy will be the team names. The post type will be the pages for these teams.

Everything works fine by default, but I want to display these as a specific hierarchical url like so:

/team/team_tax/post

To get the team_tax permalink to behave like this I used the following plugin: WP Better Permalinks. This adds the team_tax to the url.

Now eventually I want to create a structure like so:

/team/example-team-1/info
/team/example-team-1/members
/team/example-team-1/schedule
/team/example-team-2/info
/team/example-team-2/members
/team/example-team-2/schedule
etc.

Now the problem is, Wordpress doesn't recognize the pages as a child of this taxonomy. So it will start adding -2, -3 etc. to the slugs, so the following happens instead:

/team/example-team-1/info
/team/example-team-2/info-2
/team/example-team-3/info-3

I have tried to use a filter on 'wp_unique_post_slug' to ignore the newly made slug and use the default one instead. But then the page will load the content from all these 3 pages. Also a bit more simplified for demonstration:

function team_non_unique_post_slug( $slug, $original_slug ) {
  if( $post_type === 'team' ) {
   return $original_slug;
  }
}
add_filter( 'wp_unique_post_slug', 'team_non_unique_post_slug' );

So my question is, is there a way for me to achieve this without any conflicts? Basically it should be behaving as the hierachical structure of default Wordpress pages, but with a taxonomy instead. I'm also open for better suggestions/solutions!

Edit:

function alter_query_for_duplicates( $query ) {

    if ( is_admin() ) {
      return;
    }

    if ( is_singular() ) {

      if ( $query->is_main_query() ) {

        if ( in_array ( $query->get('post_type'), array('team') ) ) {

            $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

            $split_url = explode( "/", $actual_link );

            $split_url = array_filter( $split_url );

            $url_tax = $split_url[count($split_url)-1];
            $url_page = end($split_url);

            $args = array(
                'posts_per_page' => -1,
                'post_type' => 'team',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'team_tax',
                        'field' => 'slug',
                        'terms' => $url_tax,
                        'operator' => 'IN'
                    )
                ),
                // 'name' => $url_page (doesn't work because of duplicate slugs)
            );
            $posts_array = get_posts( $args );

            $current_post_key = search_for_name($url_page, $posts_array);

            $query->set( 'page_id', $posts_array[$current_post_key]->ID );

        }

      }

    }

  return $query;

}
add_filter( 'pre_get_posts', 'alter_query_for_duplicates' );


function search_for_name($name, $array) {

   foreach ($array as $key => $val) {
       if ($val->post_name === $name) {
           return $key;
       }
   }
   return null;

}

I currently added this extremely hacky way to get the content based on the current page url. It will get all posts from the specified taxonomy, and then search for the page slug in these posts and set the pre_get_posts query to only search for this page's ID.

Post a comment

comment list (0)

  1. No comments so far