douyao1994 2019-04-29 01:56 采纳率: 100%
浏览 46

如何有效地查询具有多个条件的帖子?

I have call-to-action posts that I want to display on my front page. I'd like to query these custom post types and display one of them that has a launch date < today's date, and an expiry date > today's date. If there aren't any that meet these conditions, I want to display a call-to-action that doesn't have an expiry date.

Is there a way I can query all call-to-action posts, then randomly display one that meets the first condition, and then if empty, the second condition?

I've successfully tried querying twice, one for each condition (see below). But I think I'd rather query the database once. As this way I can add further conditions as needed, without making innumerable queries. Or would this approach be ill-advised?

$today = date( "Ymd" );

// args
$condition1 = array(
    'posts_per_page'    => 1,
    'post_type'     => 'cta',
    'post-status'       => 'publish',
    'orderby'       => 'rand',
        'meta_query'        =>array(
            'relation' => 'AND',
            array(
                'key' => 'launch',
                'value' => $today,
                'compare' => '<'
            ),
                array(
                'key' => 'expiry',
                'value' => $today,
                'compare' => '>'
            ),
    )
);

$condition2 = array(
    'posts_per_page'    => 1,
    'post_type'     => 'cta',
    'post-status'       => 'publish',
    'orderby'       => 'rand',
    'meta_query'        =>array(
            array(
                'key' => 'launch',
                'value' => $today,
                'compare' => '<'
            ),
    )
);

// query
$cta1 = new WP_Query( $condition1 );
$cta2 = new WP_Query( $condition2 );

    if( !empty($cta1 -> have_posts()) ) :

        while( $cta1 -> have_posts() ) : $cta1 -> the_post(); global $post;

        // Display post with first condition

        endwhile; wp_reset_postdata();


    elseif( ( $cta2 -> have_posts() ) ) :

        while( $cta2 -> have_posts() ) : $cta2 -> the_post(); global $post;

        // Display post with second condition

        endwhile; wp_reset_postdata();

    endif;
  • 写回答

1条回答 默认 最新

  • dsirr48088 2019-04-29 03:50
    关注

    You can use the advanced meta query option

    $meta_query = array(
        'posts_per_page'    => 1,
        'post_type'     => 'cta',
        'post-status'       => 'publish',
        'orderby'       => 'rand',
            'meta_query'        =>array(
                'relation' => 'OR',
                array(
                    'key' => 'launch',
                    'value' => $today,
                    'compare' => '<'
                ),
                array(
                    'relation' => 'AND',
                    array(
                        'key' => 'launch',
                        'value' => $today,
                        'compare' => '<'
                    ),
                    array(
                        'key' => 'expiry',
                        'value' => $today,
                        'compare' => '>'
                    ),
                ),
        )
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?