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

categories - Query post by Category and custom file (ACF)

programmeradmin0浏览0评论

Well, I try to create a query that will only take posts if the conditions are met, Must be in this category (Event) and have the selected type (News-checkbox). First I asked on the ACF forum but the only answer I got was sent back here: /

I tried but I can not make it work properly.

                        <?php 

                            $args = array(
                                'showposts' => 6,
                                'post_type'     => 'post',
                                'meta_query'    => array(
                                    'relation'      => 'AND',
                                    array(
                                        'key'       => 'type_id',
                                        'value'     => 'News',
                                        'compare'   => 'LIKE'
                                    ),
                                    array(
                                        'category_name' => 'event',
                                        'compare'       =>  'LIKE'
                                    )
                                )
                            );  
            $the_query = new WP_Query( $args );
            ?>

I try this as well

$args = array(
  'post_type' => 'post', 
  'tax_query' => array(
    array(
     'category_name' => 'event',
    )
  ),
  'meta_query' => array(
    array(
      'key' => 'type_id',
      'value' => 'News'
    )
  )
)

Well, I try to create a query that will only take posts if the conditions are met, Must be in this category (Event) and have the selected type (News-checkbox). First I asked on the ACF forum but the only answer I got was sent back here: https://support.advancedcustomfields/forums/topic/select-posts-by-category-custom-field/

I tried but I can not make it work properly.

                        <?php 

                            $args = array(
                                'showposts' => 6,
                                'post_type'     => 'post',
                                'meta_query'    => array(
                                    'relation'      => 'AND',
                                    array(
                                        'key'       => 'type_id',
                                        'value'     => 'News',
                                        'compare'   => 'LIKE'
                                    ),
                                    array(
                                        'category_name' => 'event',
                                        'compare'       =>  'LIKE'
                                    )
                                )
                            );  
            $the_query = new WP_Query( $args );
            ?>

I try this as well

$args = array(
  'post_type' => 'post', 
  'tax_query' => array(
    array(
     'category_name' => 'event',
    )
  ),
  'meta_query' => array(
    array(
      'key' => 'type_id',
      'value' => 'News'
    )
  )
)
Share Improve this question asked May 9, 2019 at 12:11 Jakub KrzyżanowskiJakub Krzyżanowski 73 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You make a few mistakes in your code.

To query posts by category you can use category_name [ref] or tax_query [ref] in query parameters.

// category_name / cat / category__and
'category_name' => 'event',
'tax_query' => [
    [
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => 'event',
    ]
],

ACF checkbox are stored in custom field, as serialized array. For this reason, the value you are looking for ("News") should be surrounded by double quotes.

'meta_query' => array(
    'key'       => 'type_id',  // custom field id
    'value'     => '"News"',   // custom field value
    'compare'   => 'LIKE'
),

Complete args:

$args = [
    'showposts'     => 6,
    'post_type'     => 'post',
    'category_name' => 'event',     // <-- category slug
    'meta_query'    => [
        [
            'key'     => 'type_id', // <-- ACF field id
            'value'   => '"News"',  // <-- ACF field value
            'compare' => 'LIKE'
        ] 
    ],
];
$the_query = new WP_Query( $args );
发布评论

评论列表(0)

  1. 暂无评论