admin管理员组文章数量:1122846
I am trying to access post meta for custom post type and taxonomy using WP_Query and then query the posts with that specific post meta.
So far I have tied the following code:
$hot_args = array(
'post_type' => 'video',
'posts_per_page' => '6',
"order" => "DESC",
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'date_query' => array(
array(
'after' => '1 week ago'
)
),
"post__not_in" => $posts__not_in,
'tax_query' => array(
array(
'taxonomy' => 'video_cat',
'field' => 'slug',
'terms' => "all"
)
),
);
$hot_query = new WP_Query( $hot_args );
This code doesn't work and not returning any results.
For normal posts this piece of code works but for custom post type doesn't, How can I make it work for custom post types?
I am trying to access post meta for custom post type and taxonomy using WP_Query and then query the posts with that specific post meta.
So far I have tied the following code:
$hot_args = array(
'post_type' => 'video',
'posts_per_page' => '6',
"order" => "DESC",
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'date_query' => array(
array(
'after' => '1 week ago'
)
),
"post__not_in" => $posts__not_in,
'tax_query' => array(
array(
'taxonomy' => 'video_cat',
'field' => 'slug',
'terms' => "all"
)
),
);
$hot_query = new WP_Query( $hot_args );
This code doesn't work and not returning any results.
For normal posts this piece of code works but for custom post type doesn't, How can I make it work for custom post types?
3 Answers
Reset to default 1Find The problem ;)
The answer is
$hot_args = array(
'post_type' => 'video',
'posts_per_page' => '6',
"order" => "DESC",
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'date_query' => array(
array(
'after' => '1 week ago'
)
),
"post__not_in" => $posts__not_in,
);
$hot_query = new WP_Query( $hot_args );
There was not video as my custom post type exists in the past week so the there was nothing to return :)
You should not use post meta in your case. This will result in slow database query performance. Use a custom table instead.
Have a look at one of these:
- Would this post meta be better added to the post table rather than post_meta table
The solution is :)
// Define an array of post IDs to exclude
$posts__not_in = array( /* Add your post IDs to exclude here */ );
// Define the arguments for WP_Query
$hot_args = array(
'post_type' => 'video', // Retrieve posts of type 'video'
'posts_per_page' => 6, // Limit to 6 posts per page
'order' => 'DESC', // Order by descending order
'orderby' => 'meta_value_num', // Order by meta value (numeric)
'meta_key' => 'post_views_count', // Sort by post views count
'date_query' => array( // Filter by date
array(
'after' => '1 week ago', // Show posts published after 1 week ago
),
),
'post__not_in' => $posts__not_in, // Exclude posts defined in $posts__not_in array
'tax_query' => array( // Filter by taxonomy
array(
'taxonomy' => 'video_cat', // Use 'video_cat' taxonomy
'field' => 'slug', // Use slug for field matching
'terms' => 'all', // Show posts in 'all' category
'operator' => 'IN', // Match terms using IN operator
),
),
);
// Create a new WP_Query instance with the updated arguments
$hot_query = new WP_Query( $hot_args );
本文标签: wp queryHow to get post meta for custom post type and taxonomy
版权声明:本文标题:wp query - How to get post meta for custom post type and taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310466a1934385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论