1

Imagine I have a custom post type called City with custom related meta.

The following is working just fine:

mysite.com/city/new-york/

However, I would like to have an "inner" page listing stuff related to that post in the following way:

mysite.com/city/new-york/stores

Inside of which I would query for something like

post_type='stores',
category='new-york'

If an editor adds another city it should work with no additional coding.

And then hopefully have posts linked as:

mysite.com/city/new-york/stores/apple

I've been searching for this for a while and still haven't figured it out. Maybe I'm approaching it the wrong way.

Asfar as I know, mysite.com/city/new-york can't be a custom taxonomy because I need it to be a full featured page, with meta boxes and featured image.

1 Answer 1

0

Register your taxonomy first and set the argument slug and rewrite rules to your slug which is city

Register your post type with slug of your taxonomy city prefixed and make sure you set the has_archive argument to taxonomy.

Now add a filter to post_type_link to make the stores show the individual permalink.

register_post_type(
    'city',
    array(
        'rewrite' => array( 'slug' => 'city/%stores%', 'with_front' => false ),
       'has_archive' => 'city',
       // additional args
    )
);


register_taxonomy(
  'stores',
  'city',
   array(
    'rewrite' => array( 'slug' => 'city', 'with_front' => false ),
    // your other args...
)
);

function yourprefix_store_permalinks( $post_link, $post ){
  if ( is_object( $post ) && $post->post_type == 'city' ){
      $terms = wp_get_object_terms( $post->ID, 'stores' );
      if( $terms ){
          return str_replace( '%stores%' , $terms[0]->slug , $post_link );
      }
  }
  return $post_link;
}
add_filter( 'post_type_link', 'yourprefix_store_permalinks', 1, 2 );

Use built-in Tools Generating WP Stuff here at GenrateWP

Hope this is what you are looking for. If not you can read about similar article by https://wordpress.stackexchange.com/questions/199456/custom-taxonomy-post-slug-permalink

4
  • Did as you said and it's now mysite.com/city/%stores%/new-york and mysite.com/city/stores/new-york when it should be mysite.com/city/new-york/ and mysite.com/city/new-york/stores respectively. Thanks a lot for the effort though Jan 9, 2017 at 4:43
  • What are the following can you please correct again so that I can cross-check and try city / new-york / stores / I think City is a PostType and New York is a City / stores is a single post.
    – Maqk
    Jan 9, 2017 at 5:58
  • city is indeed a post type. new-york, though, is a single post of such post type and stores should be some kind of "taxonomy" linking to a page where I could list anything related to new-york. I think I finally found a way to do it: link Jan 9, 2017 at 7:03
  • Glad you got it working. Anyway if you post how you got it. then it will be helpful for others too.
    – Maqk
    Jan 9, 2017 at 7:32

Your Answer

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

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