- function register_books_cpt() {
- $cpt_args = array(
- 'label' => 'Books',
- 'public' => true,
- 'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
- 'menu_icon' => 'dashicons-book',
- 'has_archive' => true,
- 'rewrite' => array('slug' => 'books'),
- );
- register_post_type('books', $cpt_args);
- $tax_args = array(
- 'label' => 'Genres',
- 'public' => true,
- 'hierarchical' => true,
- 'rewrite' => array('slug' => 'genres'),
- );
- register_taxonomy('genres', 'books', $tax_args);
- }
- add_action('init', 'register_books_cpt_taxonomy');
- $args = array(
- 'post_type' => 'books',
- 'tax_query' => array(
- 'relation' => 'AND',
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fiction',
- ),
- array(
- 'taxonomy' => 'authors',
- 'field' => 'slug',
- 'terms' => 'j-k-rowling',
- ),
- ),
- );
- $args = array(
- 'post_type' => 'books',
- 'meta_query' => array(
- array(
- 'key' => 'publication_year',
- 'value' => '2020',
- 'compare' => '>=',
- 'type' => 'NUMERIC',
- ),
- ),
- );
- $args = array(
- 'post_type' => 'books',
- 'tax_query' => array(
- 'relation' => 'AND',
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fiction',
- ),
- array(
- 'taxonomy' => 'authors',
- 'field' => 'slug',
- 'terms' => 'j-k-rowling',
- ),
- ),
- 'meta_query' => array(
- array(
- 'key' => 'rating',
- 'value' => '4.5',
- 'compare' => '>=',
- 'type' => 'NUMERIC',
- ),
- ),
- );
- $args = array(
- 'post_type' => 'books',
- 'tax_query' => array(
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fantasy',
- 'include_children' => false,
- ),
- ),
- );
- tax_query Relations
- 'tax_query' => array(
- 'relation' => 'AND',
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fiction',
- ),
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fantasy',
- ),
- );
- 'tax_query' => array(
- 'relation' => 'OR',
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fiction',
- ),
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fantasy',
- ),
- );
- Nested Relations:
- 'tax_query' => array(
- 'relation' => 'OR',
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fiction',
- ),
- array(
- 'relation' => 'AND',
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'fantasy',
- ),
- array(
- 'taxonomy' => 'genres',
- 'field' => 'slug',
- 'terms' => 'adventure',
- ),
- ),
- );
- meta_query Parameters
- Equality Comparison:
- = (Default): Equal to.
- !=: Not equal to.
- Example: Fetch books with a rating of exactly 4.5.
- php
- Copy code
- 'meta_query' => array(
- array(
- 'key' => 'rating',
- 'value' => '4.5',
- 'compare' => '=',
- ),
- );
- Range Comparison:
- >: Greater than.
- >=: Greater than or equal to.
- <: Less than.
- <=: Less than or equal to.
- Example: Fetch books with a rating of 4.5 or higher.
- php
- Copy code
- 'meta_query' => array(
- array(
- 'key' => 'rating',
- 'value' => '4.5',
- 'compare' => '>=',
- ),
- );
- Pattern Matching:
- LIKE: Value contains the given string.
- NOT LIKE: Value does not contain the string.
- Example: Fetch books where the description includes "bestseller".
- 'meta_query' => array(
- array(
- 'key' => 'description',
- 'value' => 'bestseller',
- 'compare' => 'LIKE',
- ),
- );
- List Comparison:
- IN: Value is in a list.
- NOT IN: Value is not in a list.
- Example: Fetch books with ratings of 4, 4.5, or 5.
- 'meta_query' => array(
- array(
- 'key' => 'rating',
- 'value' => array('4', '4.5', '5'),
- 'compare' => 'IN',
- ),
- );
- Null/Existence Check:
- EXISTS: Field exists.
- NOT EXISTS: Field does not exist.
- Example: Fetch books that have a custom field rating.
- 'meta_query' => array(
- array(
- 'key' => 'rating',
- 'compare' => 'EXISTS',
- ),
- );
- type Options
- Defines the data type for comparison. Available options are:
- NUMERIC:
- For numeric values.
- Example: Fetch books with a rating of 4.5 or higher.
- 'meta_query' => array(
- array(
- 'key' => 'rating',
- 'value' => '4.5',
- 'compare' => '>=',
- 'type' => 'NUMERIC',
- ),
- );
- CHAR (Default):
- For character-based data.
- Example: Fetch books with ISBN containing "978".
- 'meta_query' => array(
- array(
- 'key' => 'isbn',
- 'value' => '978',
- 'compare' => 'LIKE',
- 'type' => 'CHAR',
- ),
- );
- DATE:
- For date values.
- Example: Fetch books published after 2020-01-01.
- 'meta_query' => array(
- array(
- 'key' => 'publication_date',
- 'value' => '2020-01-01',
- 'compare' => '>',
- 'type' => 'DATE',
- ),
- );
- DATETIME:
- For datetime values.
- Example: Fetch books added to the database after 2023-01-01 12:00:00.
- 'meta_query' => array(
- array(
- 'key' => 'added_date',
- 'value' => '2023-01-01 12:00:00',
- 'compare' => '>',
- 'type' => 'DATETIME',
- ),
- );
- Combining Multiple meta_query Clauses
- You can use multiple meta queries with relation.
- Example: Fetch books with a rating of 4.5 or higher AND published after 2020.
- 'meta_query' => array(
- 'relation' => 'AND',
- array(
- 'key' => 'rating',
- 'value' => '4.5',
- 'compare' => '>=',
- 'type' => 'NUMERIC',
- ),
- array(
- 'key' => 'publication_date',
- 'value' => '2020-01-01',
- 'compare' => '>',
- 'type' => 'DATE',
- ),
- ),
function register_books_cpt() {
$cpt_args = array(
'label' => 'Books',
'public' => true,
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
'menu_icon' => 'dashicons-book',
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
);
register_post_type('books', $cpt_args);
$tax_args = array(
'label' => 'Genres',
'public' => true,
'hierarchical' => true, // Acts like categories (true) or tags (false)
'rewrite' => array('slug' => 'genres'),
);
register_taxonomy('genres', 'books', $tax_args);
}
add_action('init', 'register_books_cpt_taxonomy');
$args = array(
'post_type' => 'books',
'tax_query' => array(
'relation' => 'AND', // Combine conditions
array(
'taxonomy' => 'genres',
'field' => 'slug', // Can also use 'term_id'
'terms' => 'fiction', // Slug of the term
),
array(
'taxonomy' => 'authors',
'field' => 'slug',
'terms' => 'j-k-rowling',
),
),
);
$args = array(
'post_type' => 'books',
'meta_query' => array(
array(
'key' => 'publication_year',
'value' => '2020',
'compare' => '>=',
'type' => 'NUMERIC',
),
),
);
$args = array(
'post_type' => 'books',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'fiction',
),
array(
'taxonomy' => 'authors',
'field' => 'slug',
'terms' => 'j-k-rowling',
),
),
'meta_query' => array(
array(
'key' => 'rating',
'value' => '4.5',
'compare' => '>=',
'type' => 'NUMERIC',
),
),
);
$args = array(
'post_type' => 'books',
'tax_query' => array(
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'fantasy',
'include_children' => false,
),
),
);
tax_query Relations
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'fiction',
),
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'fantasy',
),
);
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'fiction',
),
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'fantasy',
),
);
Nested Relations:
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'fiction',
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'fantasy',
),
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => 'adventure',
),
),
);
meta_query Parameters
Equality Comparison:
= (Default): Equal to.
!=: Not equal to.
Example: Fetch books with a rating of exactly 4.5.
php
Copy code
'meta_query' => array(
array(
'key' => 'rating',
'value' => '4.5',
'compare' => '=',
),
);
Range Comparison:
>: Greater than.
>=: Greater than or equal to.
<: Less than.
<=: Less than or equal to.
Example: Fetch books with a rating of 4.5 or higher.
php
Copy code
'meta_query' => array(
array(
'key' => 'rating',
'value' => '4.5',
'compare' => '>=',
),
);
Pattern Matching:
LIKE: Value contains the given string.
NOT LIKE: Value does not contain the string.
Example: Fetch books where the description includes "bestseller".
'meta_query' => array(
array(
'key' => 'description',
'value' => 'bestseller',
'compare' => 'LIKE',
),
);
List Comparison:
IN: Value is in a list.
NOT IN: Value is not in a list.
Example: Fetch books with ratings of 4, 4.5, or 5.
'meta_query' => array(
array(
'key' => 'rating',
'value' => array('4', '4.5', '5'),
'compare' => 'IN',
),
);
Null/Existence Check:
EXISTS: Field exists.
NOT EXISTS: Field does not exist.
Example: Fetch books that have a custom field rating.
'meta_query' => array(
array(
'key' => 'rating',
'compare' => 'EXISTS',
),
);
type Options
Defines the data type for comparison. Available options are:
NUMERIC:
For numeric values.
Example: Fetch books with a rating of 4.5 or higher.
'meta_query' => array(
array(
'key' => 'rating',
'value' => '4.5',
'compare' => '>=',
'type' => 'NUMERIC',
),
);
CHAR (Default):
For character-based data.
Example: Fetch books with ISBN containing "978".
'meta_query' => array(
array(
'key' => 'isbn',
'value' => '978',
'compare' => 'LIKE',
'type' => 'CHAR',
),
);
DATE:
For date values.
Example: Fetch books published after 2020-01-01.
'meta_query' => array(
array(
'key' => 'publication_date',
'value' => '2020-01-01',
'compare' => '>',
'type' => 'DATE',
),
);
DATETIME:
For datetime values.
Example: Fetch books added to the database after 2023-01-01 12:00:00.
'meta_query' => array(
array(
'key' => 'added_date',
'value' => '2023-01-01 12:00:00',
'compare' => '>',
'type' => 'DATETIME',
),
);
Combining Multiple meta_query Clauses
You can use multiple meta queries with relation.
Example: Fetch books with a rating of 4.5 or higher AND published after 2020.
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'rating',
'value' => '4.5',
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'publication_date',
'value' => '2020-01-01',
'compare' => '>',
'type' => 'DATE',
),
),