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?
I'm not very sure of what you're trying to achieve, but I hope this answer can help. :)
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]
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>';
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.
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:09WP::$matched_rule
. – Sally CJ Commented Oct 27, 2018 at 15:33global $wp; echo $wp->matched_rule;
orecho $GLOBALS['wp']->matched_rule;
– Sally CJ Commented Oct 27, 2018 at 16:14is_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