$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'); ?>custom post types - How do I know if a rewritten rule was applied?|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)

custom post types - How do I know if a rewritten rule was applied?

matteradmin10PV0评论

I defined a custom post type with rewrite rule:

    register_post_type('balls', [
        'labels' => [
            'name' => 'balls',
            'singular_name' => 'balls',
            'add_new' => 'new',
            'add_new_item' => 'new',
            'parent_item_colon' => ''
        ],
        'taxonomies' => ['category'],
        'menu_position' => 4,
        'public' => true,
        'query_var' => true,
        'capability_type' => 'post',
        'supports' => ['title', 'editor', 'thumbnail'],
        'rewrite' => [
            'slug' => 'ballinfo'
        ]
    ]);

now how do I know if a site was called with /ballinfo or not? How do I know if my rewrite rule was matched and used?

I defined a custom post type with rewrite rule:

    register_post_type('balls', [
        'labels' => [
            'name' => 'balls',
            'singular_name' => 'balls',
            'add_new' => 'new',
            'add_new_item' => 'new',
            'parent_item_colon' => ''
        ],
        'taxonomies' => ['category'],
        'menu_position' => 4,
        'public' => true,
        'query_var' => true,
        'capability_type' => 'post',
        'supports' => ['title', 'editor', 'thumbnail'],
        'rewrite' => [
            'slug' => 'ballinfo'
        ]
    ]);

now how do I know if a site was called with /ballinfo or not? How do I know if my rewrite rule was matched and used?

Share Improve this question asked Oct 27, 2018 at 14:03 John SmithJohn Smith 1036 bronze badges 7
  • 1 Go to wp-admin/edit.php?post_type=balls and mouseover the "View" link of any post, and see if the permalink has /ballinfo/{slug}. If yes, the rewrite rule is being applied. Then visit the permalink and if you see the proper post/content, the rewrite rule works properly. If not, visit the permalink settings page to flush the rewrite rules. – Sally CJ Commented Oct 27, 2018 at 15:09
  • 1 If you meant by programmatically, you can get the rewrite rule that matches the requested URL via WP::$matched_rule. – Sally CJ Commented Oct 27, 2018 at 15:33
  • 1 Use either global $wp; echo $wp->matched_rule; or echo $GLOBALS['wp']->matched_rule; – Sally CJ Commented Oct 27, 2018 at 16:14
  • 1 There's probably an API function to accomplish what you are trying to do, like is_singular( 'balls' );, but you have not described the problem you are trying to solve or given any details, like where and when you are trying to do this. – Milo Commented Oct 27, 2018 at 16:23
  • 1 @JohnSmith, check my answer. – Sally CJ Commented Oct 28, 2018 at 7:13
 |  Show 2 more comments

1 Answer 1

Reset to default 1

I'm not very sure of what you're trying to achieve, but I hope this answer can help. :)

  1. With the way your custom post type is being registered, WordPress will create custom rewrite rules such as below for the CPT's single post pages:

    RegEx: ballinfo/([^/]+)(?:/([0-9]+))?/?$
    Query: index.php?balls=$matches[1]&page=$matches[2]
    
  2. So for the following question:

    how do I know if a site was called with /ballinfo or not

    When the single page of a balls post is visited, the URL would have /ballinfo/ as in example/ballinfo/an-example-balls-post.

    Now to programmatically check if the URL contains /ballinfo/, you can check if the $request property of the global WP class instance starts with a ballinfo/ like so:

    global $wp;
    if ( preg_match( '#^ballinfo/#', $wp->request ) ) {
        echo 'Site was called with the /ballinfo<br>';
    }
    echo '$wp->request is ' . $wp->request . '<br>';
    
  3. And for the following question:

    How do I know if my rewrite rule was matched and used?

    You can match the rule (RegEx) with the one in the $matched_rule property of the global WP class instance.

    For example, for the single balls post pages, where the rule uses the RegEx pattern as in point #1 in this answer, try this:

    global $wp;
    if ( 'ballinfo/([^/]+)(?:/([0-9]+))?/?$' === $wp->matched_rule ) {
        echo 'Yay, my rewrite rule was matched!<br>';
    } else {
        echo 'Not matched. $wp->matched_rule is ' . $wp->matched_rule . '<br>';
    }
    

And you may already know this, but if you just wanted to check if the requested URL is for a CPT post/archive/etc., you can use is_singular(), is_post_type_archive(), and other appropriate WordPress conditional functions/tags.

Post a comment

comment list (0)

  1. No comments so far