$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Rewrite of Custom Post Type doesn't work with dynamic data|Programmer puzzle solving
最新消息: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)

url rewriting - Rewrite of Custom Post Type doesn't work with dynamic data

matteradmin8PV0评论

I created a Custom Post Type in my plugin. I added an argument with rewrite:

'rewrite' => array('slug' => 'foo/bar/%baz%', 'with_front' => false),

Then I added a method to replace %baz%:

public function customPermalinks($post_link, $post)
{
    if (is_object($post) && $post->post_type == 'test') {
        $terms = wp_get_object_terms($post->ID, 'baz');
        if ($terms) {
            return str_replace('%baz%', $terms[0]->slug, $post_link);
        }
    }
    return $post_link;
}

On __construct() I added this filter:

add_filter('post_type_link', [$this, 'customPermalinks'], 1, 2);

And... My CPT works only when I don't use dynamic variables (%baz%). When I remove %baz% from rewrite attribute, it works... Otherwise, I get a 404 message when I want to go to a single post. How to fix it?

I will add that before each change I manually refresh rewrite rules.

Here is my plugin file structure:

class PluginName
{
    public function __construct()
    {
        $this->createPostType();
        add_filter('post_type_link', [$this, 'customPermalinks'], 1, 2);
    }

    private function createPostType()
    {
        // arguments to my CPT
    }

    public function customPermalinks($post_link, $post)
    {
        // replace dynamic data from $post_link
    }
}

$pluginName = new PluginName();

I created a Custom Post Type in my plugin. I added an argument with rewrite:

'rewrite' => array('slug' => 'foo/bar/%baz%', 'with_front' => false),

Then I added a method to replace %baz%:

public function customPermalinks($post_link, $post)
{
    if (is_object($post) && $post->post_type == 'test') {
        $terms = wp_get_object_terms($post->ID, 'baz');
        if ($terms) {
            return str_replace('%baz%', $terms[0]->slug, $post_link);
        }
    }
    return $post_link;
}

On __construct() I added this filter:

add_filter('post_type_link', [$this, 'customPermalinks'], 1, 2);

And... My CPT works only when I don't use dynamic variables (%baz%). When I remove %baz% from rewrite attribute, it works... Otherwise, I get a 404 message when I want to go to a single post. How to fix it?

I will add that before each change I manually refresh rewrite rules.

Here is my plugin file structure:

class PluginName
{
    public function __construct()
    {
        $this->createPostType();
        add_filter('post_type_link', [$this, 'customPermalinks'], 1, 2);
    }

    private function createPostType()
    {
        // arguments to my CPT
    }

    public function customPermalinks($post_link, $post)
    {
        // replace dynamic data from $post_link
    }
}

$pluginName = new PluginName();
Share Improve this question edited Feb 9, 2019 at 19:05 Brick asked Feb 9, 2019 at 17:53 BrickBrick 11 bronze badge 3
  • Does baz belong to a registered object, or did you add baz to the list of valid query vars? – Milo Commented Feb 9, 2019 at 18:11
  • @Milo baz is registered custom taxonomy. Variable $post_link returns a valid rewrite with baz value, ex. foo/bar/lorem-ipsum/custom-post-title but when I go at this address (the_permalink) it shows error with no post. – Brick Commented Feb 9, 2019 at 18:39
  • @Milo I forgot about add_rewrite_tag('%baz%', '([^&/]+)');. Problem solved! – Brick Commented Feb 9, 2019 at 19:04
Add a comment  | 

1 Answer 1

Reset to default 0

Well... I didn't know that the expressions in the rewrite attribute of CPT must be registered in advance:

add_rewrite_tag('%baz%', '([^&/]+)');
Post a comment

comment list (0)

  1. No comments so far