1

I have 2 Custom Post Type : article and multipager_article

Now I want to customize permalink structure for these 2 custom post type only

I want Structure like this :

site_url/term_slug/post_id

I tried different hooks , but no luck It works for 1 post type , and breaking other post type

 function post_type_permalink( $post_link, $post) {
  if( is_object( $post ) && $post->post_type == 'article' ){
     $terms = wp_get_object_terms( $post->ID, 'language' );
     $parameters = ['%article_tax%','%post_id%'];
     $values = [$terms[0]->slug, $post->ID];
     $post_link = str_replace($parameters,$values,$post_link);
  }
  return $post_link;
 }
 add_filter('post_link', 'post_type_permalink', 1, 3);
 add_filter('post_type_link', 'post_type_permalink', 1, 3); 

I have set rewrite slug on the article post type,

class Article extends PostTypeAbstract{
/**
 * @return array<string, mixed>
 */
public function getOptions(): array
{
    add_rewrite_tag('%article_tax%','(.+)');
    add_rewrite_tag('%post_id%','(.+)');

    return [
        'label'        => $this->getName(),
        'description'  => $this->getDescription(),
        'public'       => true,
        'show_ui'      => true,
        'show_in_rest' => true,
        'show_in_menu' => 'edit.php',
        'supports'     => ['title', 'editor', 'thumbnail', 'custom-fields'],
        'rewrite'   => array('slug' => '%article_tax%/%post_id%', 'with_front' => false)
    ];
}}

Above code, I have tried but it works for only article post type, if I set rewrite for the mutlipager_article so it is going to the 404 page.

any help would be appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.