# Cách để xem các quy luật rule lấy giá trị như thế nào add\_rewrite\_rule, $wp\_rewrite, $wp\_query->quer

```
// ===========
add_action('init', 'my_book_cpt');
function my_book_cpt() {
  $labels = array(
    'name'               => _x('Books', 'post type general name', 'your-plugin-textdomain'),
    'singular_name'      => _x('Book', 'post type singular name', 'your-plugin-textdomain'),
    'menu_name'          => _x('Books', 'admin menu', 'your-plugin-textdomain'),
    'name_admin_bar'     => _x('Book', 'add new on admin bar', 'your-plugin-textdomain'),
    'add_new'            => _x('Add New', 'book', 'your-plugin-textdomain'),
    'add_new_item'       => __('Add New Book', 'your-plugin-textdomain'),
    'new_item'           => __('New Book', 'your-plugin-textdomain'),
    'edit_item'          => __('Edit Book', 'your-plugin-textdomain'),
    'view_item'          => __('View Book', 'your-plugin-textdomain'),
    'all_items'          => __('All Books', 'your-plugin-textdomain'),
    'search_items'       => __('Search Books', 'your-plugin-textdomain'),
    'parent_item_colon'  => __('Parent Books:', 'your-plugin-textdomain'),
    'not_found'          => __('No books found.', 'your-plugin-textdomain'),
    'not_found_in_trash' => __('No books found in Trash.', 'your-plugin-textdomain'),
  );
  $args = array(
    'labels'             => $labels,
    'description'        => __('Description.', 'your-plugin-textdomain'),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'has_archive'        => false,
    // 'rewrite'            => array('slug' => 'book','with_front'=>false),
    'capability_type'    => 'post',
    'hierarchical'       => false,
    'menu_position'      => null,
    'show_in_rest'       => true,
    'rest_base'          => 'book',
    'supports'           => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
  );
  register_post_type('book', $args);
  flush_rewrite_rules(true);
}
// ===========
add_action('init', 'my_book_taxonomy', 30);
function my_book_taxonomy() {
  $labels = array(
    'name'              => _x('Genres', 'taxonomy general name'),
    'singular_name'     => _x('Genre', 'taxonomy singular name'),
    'search_items'      => __('Search Genres'),
    'all_items'         => __('All Genres'),
    'parent_item'       => __('Parent Genre'),
    'parent_item_colon' => __('Parent Genre:'),
    'edit_item'         => __('Edit Genre'),
    'update_item'       => __('Update Genre'),
    'add_new_item'      => __('Add New Genre'),
    'new_item_name'     => __('New Genre Name'),
    'menu_name'         => __('Genre'),
  );
  $args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array('slug' => 'genre'),
    'show_in_rest'      => true,
    'rest_base'         => 'genre',
  );
  register_taxonomy('genre', array('book'), $args);
  flush_rewrite_rules(true);
}
// ====
add_action( 'init', 'pmg_rewrite_add_rewrites' );
function pmg_rewrite_add_rewrites() {
  add_rewrite_rule( 'myparamname/([a-z0-9-]+)[/]?$', 'index.php?myparamname=$matches[1]', 'top' );
}
add_filter( 'query_vars', function( $query_vars ) {
    $query_vars[] = 'myparamname';
    echo '<pre>';
			var_export($query_vars);
		echo '</pre>';
    return $query_vars;
	} 
);
add_action( 'template_include', function( $template ) {
	global $wp_rewrite;
	echo '<pre>';
		var_export($wp_rewrite);
	echo '</pre>';
	var_export(get_query_var( 'book'));
  if ( get_query_var( 'myparamname' ) == false || get_query_var( 'myparamname' ) == '' ) {
    return $template;
  }
  include get_template_directory() . '/template-name.php';
} );
```

**Ví dụ 2:**&#x20;

![](/files/Oh3XYidJkP8CDR1J8TyV)

![](/files/bGa3F1rO92J2qq3tCZFU)

![](/files/ivQGUj5w8oAOOZxGzLns)

C:\xampp\htdocs\test1\wp-content\themes\twentytwentyone\functions.php

```
// ====
add_filter('query_vars', function($vars) {
    $vars[] = "my_subscriber";
    echo '<pre>';
    	var_export( $vars);
    echo '</pre>';
    return $vars;
});
add_action('init', function() {
    $page_id = 2; // update 2 (sample page) to your custom page ID where you can get the subscriber(s) data later
    $page_data = get_post( $page_id ); 
    add_rewrite_rule(
        $page_data->post_name . '/subscriber/([^/]+)/?$',
        'index.php?pagename=' . $page_data->post_name . '&my_subscribers=100&my_subscriber=gggg',
        'top'
    );
});
```

C:\xampp\htdocs\test1\wp-content\themes\twentytwentyone\header.php

```
<?php  
echo '<pre>';
  echo "==============1";
  var_export(get_query_var( 'my_subscriber'));
  echo "==============2";
echo '</pre>';
?>
```

**Ví dụ 3: Using Custom Templates with custom querystring**

```
/**
 * Template Name: Nutritional Information
 */
get_header(); 
 
global $wp_query;
echo 'Food : ' . $wp_query->query_vars['food'];
echo '<br />';
echo 'Variety : ' . $wp_query->query_vars['variety'];
// ... more ...
get_footer();
```

> **Xem thêm tại:**
>
> [**https://developer.wordpress.org/reference/functions/add\_rewrite\_rule/**](https://developer.wordpress.org/reference/functions/add_rewrite_rule/)

**Ví dụ 2: Một ví dụ viết lại đường dẫn subpage id = 2 và lấy giá trị truyền vào dạng ?avaiable1=abac1\&avaiable2=ab**

![](/files/a3xoHreq5HMbJimfPi3B)

![](/files/IB9rPcL9zNSnNPBPeQQq)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/wordpress/cach-de-xem-cac-quy-luat-rule-lay-gia-tri-nhu-the-nao-add_rewrite_rule-usdwp_rewrite-usdwp_query-gre.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
