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
1 Answer
Reset to default 0You 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 );