0

I have this table that is generated in a Custom Post Type (CPT)

enter image description here

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

2 Answers 2

0

You 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

0

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.