• I registered a post type named ‘work,’ and I need to modify the permalink structure for this post type to an string consisting of 18 characters. I use the following code to rewrite the rules, but when I open a ‘work’ post page, it goes to the ‘work’ archive page instead. I don’t know where the issue is.

    register post type:

    <?php
    namespace BLANPI\App;
    defined('ABSPATH') || exit;
    use BLANPI\App\Traits\Singleton;
    
    class Blanpi_Post_Type
    {
      use Singleton;
      protected function __construct()
      {
        $this->setup_hooks();
      }
      protected function setup_hooks()
      {
        add_action('init', [$this, 'blanpi_create_post_type']);
      }
      public static function blanpi_create_post_type()
      {
        $work_args = array(
          'labels' => array(
            'name' => __('Work', 'blanpi'),
            'singular_name' => __('Work', 'blanpi'),
            'menu_name' => __('Work', 'blanpi'), 'admin menu',
            'name_admin_bar' => __('Work', 'blanpi'),
          ),
          'public' => true,
          'publicly_queryable' => true,
          'show_ui' => true,
          'show_in_menu' => true,
          'show_in_nav_menus' => true,
          'query_var' => true,
          'rewrite' => true,
          'capability_type' => 'post',
          'has_archive' => true,
          'hierarchical' => true,
          'menu_position' => null,
          'show_in_rest' => true,
          'supports' => array('title', 'editor', 'post-formats', 'author', 'custom-fields'),
          'menu_icon' => 'dashicons-format-gallery'
        );
        register_post_type('work', $work_args);
      }
    }

    rewrite:

    <?php
    namespace BLANPI\App;
    defined('ABSPATH') || exit;
    use BLANPI\App\Traits\Singleton;
    class Blanpi_Setup
    {
      use Singleton;
      protected function __construct()
      {
        $this->setup_hooks();
      }
      protected function setup_hooks()
      {
        add_filter('post_link', [$this, 'custom_permalink'], 10, 3);
        add_filter('post_type_link', [$this, 'custom_permalink'], 10, 3);
        add_action('init', [$this, 'custom_rewrite_rules']);
      }
      public function custom_rewrite_rules()
      {
        add_rewrite_rule(
          '^work/([^/]+)/?$',
          'index.php?post_type=work&p=$matches[1]',
          'top'
        );
        add_rewrite_rule(
          '^work/([^/]+)/([^/]+)/?$',
          'index.php?post_type=work&name=$matches[2]',
          'top'
        );
      }
    
      public function custom_permalink($permalink, $post, $leavename)
      {
        if ($post && $post->post_type == 'work') {
          $permalink = home_url('/') . substr(md5($post->ID), 0, 18) . '/';
        }
        return $permalink;
      }
    }

    I want change the url link this:

    https://webiste.com/8212a829abo92822kl
Viewing 4 replies - 1 through 4 (of 4 total)
  • hello,

    Here is code which helps you to solve url issues.

    <?php
    namespace BLANPI\App;
    defined('ABSPATH') || exit;
    use BLANPI\App\Traits\Singleton;
    
    class Blanpi_Post_Type
    {
        use Singleton;
        
        protected function __construct()
        {
            $this->setup_hooks();
        }
    
        protected function setup_hooks()
        {
            add_action('init', [$this, 'blanpi_create_post_type']);
        }
    
        public static function blanpi_create_post_type()
        {
            $work_args = array(
                'labels' => array(
                    'name' => __('Work', 'blanpi'),
                    'singular_name' => __('Work', 'blanpi'),
                    'menu_name' => __('Work', 'blanpi'),
                    'name_admin_bar' => __('Work', 'blanpi'),
                ),
                'public' => true,
                'publicly_queryable' => true,
                'show_ui' => true,
                'show_in_menu' => true,
                'show_in_nav_menus' => true,
                'query_var' => true,
                'rewrite' => array('slug' => 'your-custom-slug', 'with_front' => false),
                'capability_type' => 'post',
                'has_archive' => true,
                'hierarchical' => true,
                'menu_position' => null,
                'show_in_rest' => true,
                'supports' => array('title', 'editor', 'post-formats', 'author', 'custom-fields'),
                'menu_icon' => 'dashicons-format-gallery'
            );
    
            register_post_type('work', $work_args);
        }
    }
    
    
    
    Moderator bcworkz

    (@bcworkz)

    Any time you do anything that could alter existing rewrite rules, you should always visit the permalinks settings screen. No need to change anything, loading the page is enough. This causes the rewrite rules to be regenerated so you can be sure you’re using the latest and greatest set of rules.

    Thread Starter weweloo

    (@weweloo)

    Thank you very much for your suggestions. I’ve tried these methods, but it seems that they don’t meet my requirements. I’ve registered a custom post type named “work,” and by default, the URL for the posts is like https://dotmain.com/work/post-slug. However, I wish to change the URL to something like https://dotmain.com/2828ea92b889oc78. I’m using a fixed string generated through substr(md5($post->ID), 0, 18) for encoding. Unfortunately, when I open the page, it returns a 404 not found error.

    Moderator bcworkz

    (@bcworkz)

    If you generate a hash from the post ID, it’s not a fixed string as far as rewrite rules are concerned. Because the hash for each page is different, as intended. What you want will be confused with regular post requests. Rewrite rules cannot tell the difference between a hash string and a post slug.

    You need “work” or similar fixed string that’s the same for all work post type requests. For example: https://dotmain.com/work/2828ea92b889oc78

    While it doesn’t affect the rewrite situation, out of curiosity, how are you resolving a hash request back to the associated post? Is the hash saved as post meta data?

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.