admin管理员组文章数量:1122832
I want get 3 posts from event custom post type. But i want to do that by category priority. If "featured" category has 3 upcoming event then i want to show 3 posts from it, if "featured" category doesn't have 3 upcoming events then i want to get posts from other category+posts without any category too.
The end result will return 3 upcoming event. I'm a little confuse about how can i add that condition parameter on WP_Query.
Current code(that only return maximum 3 posts from featured category)-
$events_args = array(
'post_type' => 'ctc_event',
'meta_query' => array(
array(
'key' => '_ctc_event_end_date',
'value' => date_i18n( 'Y-m-d' ),
'compare' => '>=',
'type' => 'DATE'
),
),
'meta_key' => '_ctc_event_start_date_start_time',
'meta_type' => 'DATETIME',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'ctc_event_category',
'field' => 'slug',
'terms' => 'featured',
),
),
);
$events = new WP_Query( $events_args );
Thanks in advance.
I want get 3 posts from event custom post type. But i want to do that by category priority. If "featured" category has 3 upcoming event then i want to show 3 posts from it, if "featured" category doesn't have 3 upcoming events then i want to get posts from other category+posts without any category too.
The end result will return 3 upcoming event. I'm a little confuse about how can i add that condition parameter on WP_Query.
Current code(that only return maximum 3 posts from featured category)-
$events_args = array(
'post_type' => 'ctc_event',
'meta_query' => array(
array(
'key' => '_ctc_event_end_date',
'value' => date_i18n( 'Y-m-d' ),
'compare' => '>=',
'type' => 'DATE'
),
),
'meta_key' => '_ctc_event_start_date_start_time',
'meta_type' => 'DATETIME',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'ctc_event_category',
'field' => 'slug',
'terms' => 'featured',
),
),
);
$events = new WP_Query( $events_args );
Thanks in advance.
Share Improve this question asked Jan 29, 2017 at 8:04 Arif KhanArif Khan 1031 gold badge1 silver badge4 bronze badges 01 Answer
Reset to default 0You can first get posts from your featured category and check if it has any posts or not. If yes, you will show them.
If not, you will make another query that will get 3 posts from random categories (as you described) and show them.
$events_args = array(
// include the code in your question here
);
$events = new WP_Query( $events_args );
if ($events->have_posts()){
while ($events->have_posts()) : the_post();
// your loop code here
endwhile;
}
if ((3-($events->found_posts)) > 0) {
wp_reset_postdata();
$remaining = 3 - ($events->found_posts);
$random_args = array(
// your desired array of attributes, set 'posts_per_page' to $remaining
);
$random_posts = new WP_Query($random_args);
while ($random_posts->have_posts()) : the_post();
// your loop code here
endwhile;
}
本文标签: Get post from Category by Priority
版权声明:本文标题:Get post from Category by Priority 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736286337a1927649.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论