I have this table that is generated in a Custom Post Type (CPT)
Is there an option that I can modify the query? For example, I have this post_type = 'my-type' in wp_posts table. I want to do a JOIN to have specific values with the table wp_postmeta - how can I do that? Where do I edit the query?
I want to list only posts that have an specific value in its meta, for that I need to do a join, all of this is to have control in what an specific user can see
I have this table that is generated in a Custom Post Type (CPT)
Is there an option that I can modify the query? For example, I have this post_type = 'my-type' in wp_posts table. I want to do a JOIN to have specific values with the table wp_postmeta - how can I do that? Where do I edit the query?
I want to list only posts that have an specific value in its meta, for that I need to do a join, all of this is to have control in what an specific user can see
Share Improve this question asked Jan 9, 2020 at 1:13 ChristianChristian 1011 bronze badge2 Answers
Reset to default 0You can use meta_query
property when fetching post data using new WP_Query()
$vehicles = new WP_Query([
'post_type' => 'vehicle',
'meta_query' => [
'relation' => 'AND',
[
'key' => 'wheel',
'value' => '4',
],
[
'key' => 'color',
'value' => 'blue',
]
]);
WP_Query official docs
I think using the pre_get_posts action should do the trick. Something along these lines. Modify as needed. Add to functions.php
.
function my_limit_admin_posts($query){
if ( is_admin() && $query->is_main_query() && 'your_post_type' === $query->get('post_type') && current_user_can('some_capability_or_role') ) {
$query->set( 'meta_query', array(
array(
'key' => 'your_meta_key',
'compare' => '!=',
'value' => 'some_value',
)
) );
}
}
add_action( 'pre_get_posts', 'my_limit_admin_posts' );
This previous Q&A might be relevant, Can I exclude a post by meta key using pre_get_posts function?