0

I have created a custom post type called "movies" and I have added custom taxonomy "Available for " and it contains two terms

  1. Rent
  2. sales.

Current movie post details page URL

movies/avengers-endgame/

I want URL Like

movies/sales/avengers-endgame/
movies/rent/avengers-endgame/
movies/{%selected term%}avengers-endgame/

Anyone, please suggest a possible solution to fix the above issue?

Thanks

1 Answer 1

1

You need to update below line at where you have register a custom post type using register_post_type function.

'rewrite' => array('slug' => 'movies/%cat%')

To change permalink dynamically of post type you have to add below code in functions.php file :

function change_link( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if( $post->post_type == 'movies' ) 
    {
       if ( is_object( $post ) ) {
          # assume that 'available-for' is slug of your taxonomy 
          $terms = wp_get_object_terms( $post->ID, array('available-for') );
          if ( $terms ) {
             return str_replace( '%cat%', $terms[0]->slug, $post_link );
         }
      }
    }
    return   $post_link ;
}
add_filter( 'post_type_link', 'change_link', 1, 3 );

//load the template on the new generated URL otherwise you will get 404's the page



   function generated_rewrite_rules() {
       add_rewrite_rule(
           '^movies/(.*)/(.*)/?$',
           'index.php?post_type=movies&name=$matches[2]',
           'top'
       );
    }
    add_action( 'init', 'generated_rewrite_rules' ); 
  • domain.com/movies/{%selected term%}/avengers-endgame/

To use Term first, you have change to this.

'rewrite' => array('slug' => '%cat%/movies')

function change_link( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if( $post->post_type == 'movies' ) 
    {
       if ( is_object( $post ) ) {
          # assume that 'available-for' is slug of your taxonomy 
          $terms = wp_get_object_terms( $post->ID, array('available-for') );
          if ( $terms ) {
             return str_replace( '%cat%', $terms[0]->slug, $post_link );
         }
      }
    }
    return   $post_link ;
}
add_filter( 'post_type_link', 'change_link', 1, 3 );

function movie_cpt_generating_rule($wp_rewrite) {
    $rules = array();
    $terms = get_terms( array(
        'taxonomy' => 'available-for',
        'hide_empty' => false,
    ) );

$post_type = 'movies';
foreach ($terms as $term) {    

    $rules[ $term->slug .'/movies/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&movies=$matches[1]&name=$matches[1]';

}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'movie_cpt_generating_rule');

After that, you need to flush rewrites permalinks, goto the wp-admin > Settings > permalinks. just update permalink setting using "Save Changes" button.

it'll return urls like below :

  • domain.com/{%selected term%}/movies/avengers-endgame/

Thank you!

14
  • Hello Thanks It's work if we add manuall term slug in URL
    – Rigal
    Jun 21, 2019 at 8:15
  • this didn't work for you? i can't get your point. what do you mean manual ? Jun 21, 2019 at 8:42
  • Yes it works fine I want to change link like domain.com/{%selected term%}/movies/avengers-endgame/ Is it possible? Thanks
    – Rigal
    Jun 21, 2019 at 9:23
  • yes it is possible. i have update the answer. i have add new functions in code. please check then let me know if this helps you or any confution. Jun 21, 2019 at 9:43
  • Hello Its working fine with Term first but I have not added below code "'rewrite' => array('slug' => '%cat%/movies')" because movies contains sales , rentals or both selected so I have created a manual URL and its working fine. Thanks
    – Rigal
    Jun 21, 2019 at 10:07

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.