最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How select query is generated in a Custom Post Type?

programmeradmin2浏览0评论

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 badge
Add a comment  | 

2 Answers 2

Reset to default 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

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?

发布评论

评论列表(0)

  1. 暂无评论