【问题标题】:Schedule filter with ACF使用 ACF 计划过滤器
【发布时间】:2015-10-09 15:37:20
【问题描述】:

我需要构建一个 WP_Query 来通过一些高级自定义字段过滤帖子。以下要点描述了所需的功能:

  • 今天大于 start_date 并且小于 'end_date'。
  • 如果今天是“开始日期”,则测试时间是否大于“开始时间”。
  • 如果今天是“end_date”,如果时间小于“end_time”,则发送文本。
  • 仅检索按自定义字段“优先级”排序的第一个帖子。

我正在努力构建典型的 WHERE 分句,例如:

SELECT * FROM x WHERE (
  ( 'start_date' < today AND 'end_date' > today )
  OR
  ( 'start_date' = today AND 'start_hour' < now )
  OR
  ( 'end_date' = today AND 'end_hour' > now )
)
ORDER BY y
LIMIT 0,1;

我已经考虑了一段时间,我认为可以通过在 WP_Query args 中嵌套 meta_queries 来实现...没有用(请参阅下面的 args)。

$now_date = date('Ymd');
$now_time = date('Hi');
$args = array(
    'post_type'         => 'post',
    'cat'               => 4, /* Bulletin */
    'posts_per_page'    => 1,
    'post_parent'       => 0,
    'post_status'       => 'publish',

    'meta_query' => array(
        'relation' => 'OR',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'       => 'bu_from',
                'compare'   => '<',
                'value'     => $now_date,
                'type'      => 'NUMERIC'
            ),
            array(
                'key'       => 'bu_to',
                'compare'   => '>',
                'value'     => $now_date,
                'type'      => 'NUMERIC'
            )
        ),
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'       => 'bu_from',
                'compare'   => '=',
                'value'     => $now_date,
                'type'      => 'NUMERIC'
            ),
            array(
                'key'       => 'bu_from_time',
                'compare'   => '<=',
                'value'     => $now_time,
                'type'      => 'NUMERIC'
            )
        ),
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'       => 'bu_to',
                'compare'   => '=',
                'value'     => $now_date,
                'type'      => 'NUMERIC'
            ),
            array(
                'key'       => 'bu_to_time',
                'compare'   => '>=',
                'value'     => $now_time,
                'type'      => 'NUMERIC'
            )
        )
    ),
    'meta_key'          => 'bu_priority',
    'orderby'           => 'meta_value',
    'order'             => 'ASC'
);

忽略所有元查询的请求:

/* Taken from var_dump($the_query); */
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
  FROM wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
  WHERE 1=1
    AND wp_posts.post_parent = 0
    AND ( wp_term_relationships.term_taxonomy_id IN (4) )
    AND wp_posts.post_type = 'post'
    AND ((wp_posts.post_status = 'publish'))
    AND (wp_postmeta.meta_key = 'bu_priority' )
  GROUP BY wp_posts.ID
  ORDER BY wp_postmeta.meta_value ASC
  LIMIT 0, 1
;

【问题讨论】:

    标签: advanced-custom-fields wordpress


    【解决方案1】:

    在与管理层审查了我们到底需要什么之后……我改变了这里涉及的所有逻辑。

        <!-- START: BULLETIN -->
        <?php
            $args = array(
                'post_type'         => 'post',
                'cat'               => 4, /* Bulletin */
                'posts_per_page'    => -1,
                'post_parent'       => 0,
                'post_status'       => 'publish',
    
                'orderby'           => 'ID',
                'order'             => 'DESC'
            );
    
            $the_query = new WP_Query( $args );
    
            if ( $the_query->have_posts() ) :
                // Start the Loop.
                $added_cnt = 0;
                $now = array( 'date' => date('md'), 'day' => date('N') );
                $bulletins = array(
                    '1' => array(),
                    '2' => array(),
                    '3' => array(),
                    '4' => array(),
                    '5' => array()
                );
                while ( $the_query->have_posts() ) : $the_query->the_post();
    
                    if (
                        /* Scheduled by day and month */
                        (
                            ( get_field('bu_schedule_by') == '1' )
                            &&
                            ( get_field('bu_start_month').get_field('bu_start_day') <= $now['date'] )
                            &&
                            ( get_field('bu_end_month').get_field('bu_end_day') >= $now['date'] )
                        )
                        ||
                        /* Scheduled by day of the week */
                        (
                            ( get_field('bu_schedule_by') == '2' )
                            &&
                            ( in_array( $now['day'], get_field('bu_days_to_be_seen') ) )
                        )
                    ) {
                        $bulletins[ get_field('bu_priority') ][ get_the_ID() ] = array();
                        $bulletins[ get_field('bu_priority') ][ get_the_ID() ][ 'title' ] = get_the_title();
                        $bulletins[ get_field('bu_priority') ][ get_the_ID() ][ 'subtitle' ] = get_field('bu_subtitle');
                        $bulletins[ get_field('bu_priority') ][ get_the_ID() ][ 'image' ] = get_field('bu_image');
                        $bulletins[ get_field('bu_priority') ][ get_the_ID() ][ 'content' ] = get_the_content();
                        $added_cnt++;
                    }
                endwhile;
    
                $bulletin = array();
                $bu_priority = 1;
                while ( ( $added_cnt > 0 ) && ( count($bulletin) == 0 ) ) {
                    if ( count($bulletins[$bu_priority]) > 0 ) {
                        $bulletin = reset($bulletins[$bu_priority]);
                    }
                    $bu_priority++;
                }
    
                $bu_image = $bulletin['image'];
                echo '
                    <div class="section group section_opener custom_bulletin" style="margin-top: 4px; margin-bottom: 4px; padding: 0 !important;">
                        <div id="xmas_bg" class="col span_4_of_16"'.( !empty($bu_image) ? ' style="background-image: url('.$bu_image['url'].'); background-position: 50%; background-repeat: no-repeat; margin: 40px 0; height: 178px;"' : '' ).'></div>
                        <div id="xmas_title" class="col span_4_of_16" style="">
                            '.( $bulletin['title'] != '' ? '<h1 style="font-size: 28px; font-weight: 700;">'.$bulletin['title'].'</h1>' : '' ).'
                            '.( $bulletin['subtitle'] != '' ? '<h2 style="font-size: 23px; color: #c94446; font-weight: 300; margin-top: -8px;">'.$bulletin['subtitle'].'</h2>' : '' ).'
                        </div>
                        <div class="col span_1_of_16"></div>
                        <div id="xmas_copy" class="col span_6_of_16" style="padding: 80px 0; font-size: 14px !important;">
                            '.$bulletin['content'].'
                        </div>
                        <div class="col span_1_of_16"></div>
                    </div>
                ';
    
            endif;
        ?>
        <!-- END: BULLETIN -->
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-24
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2023-03-14
      相关资源
      最近更新 更多