Get posts by meta key and meta value in WordPress
In this blog i will show you how to get posts in wordpress using meta_key and meta_value. For this I will pass meta_query as arguments in WP_Query function to get posts with post type.
Also Read: Get custom post types in wordpress
I have created a custom post type `books` using post type generator. you can create custom post type with post type generator and add that code in your functions.php. Use below code to get posts using meta_key and meta_value.
$args = array(
'post_type' => 'books',
'posts_per_page' => 4,
'meta_query' => array(
array(
'key' => 'book_type',
'value' => 'pdf',
),
)
);
$query = new WP_Query( $args );
if ( $query->posts ) {
foreach ( $query->posts as $key => $post_id ) {
get_the_title();
}
}
In this blog i will show you how to get posts in wordpress using meta_key and meta_value. For this I will pass meta_query as arguments in WP_Query function to get posts with post type.
Also Read: Get custom post types in wordpress
I have created a custom post type `books` using post type generator. you can create custom post type with post type generator and add that code in your functions.php. Use below code to get posts using meta_key and meta_value.
$args = array(
'post_type' => 'books',
'posts_per_page' => 4,
'meta_query' => array(
array(
'key' => 'book_type',
'value' => 'pdf',
),
)
);
$query = new WP_Query( $args );
if ( $query->posts ) {
foreach ( $query->posts as $key => $post_id ) {
get_the_title();
}
}
Recommanded Articles
- Generate Dummy Posts in WordPress
- How to add captcha in Contact Form 7
- how to create a cron job in wordpress
- How to get form data in Contact Form 7
- Insert Update Delete Select query in WordPress
- How to increase website speed
- Create plugin in WordPress in which create table in DB on activate
- Create new tab under my account WooCommerce frontend
- Custom image sizes in WordPress
- Get posts by meta key and meta value in WordPress
Latest Comments