Итак, вам нужно: get_queried_object
Функция Get Queried Object , чтобы получить текущий идентификатор категории, а затем выполнять циклы, используя класс WP_Query
вместо get_posts
. Из того, что ниже, вы сможете изменить это в соответствии со своими потребностями.
$catObject = get_queried_object();
$category = $catObject->term_id;
// WP_Query arguments for first loop
$args = array(
'posts_per_page' => '10',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $category,
),
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'custom_field_1',
'compare' => 'EXISTS',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Do your stuff with the first loop
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
// WP_Query arguments for second loop
$args2 = array(
'posts_per_page' => '10',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $category,
),
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'custom_field_2',
'compare' => 'EXISTS',
),
),
);
// The Query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post();
// Do your stuff with the second loop
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();