在WordPress 4.2.1中使用日期选择器筛选分隔符在日期之间进行查询

时间:2015-05-01 作者:Robbiegod

原职务:Query between dates using Date Picker fields

我最初在去年发表了一篇关于同一主题的帖子,但似乎是对WP v4的更新。2.1坏了我的过滤器。总之,我正在事件自定义帖子类型中使用ACF和日期选择器插件。我正在尝试查询开始日期和结束日期之间的事件。我也有可以跨越多个月的活动。例如,一项活动可能在4月开始,但在6月结束。我希望该活动在4月、5月和6月举行。

在v3上,我使用过滤器实现了这一点。wordpress的第9页。我正在尝试更新我所有网站上的wordpress,但此过滤器在更新到v4后会中断。2.1。在更新WP 4.2.1和将ACF更新到最新版本4之后,这一问题就解决了。我已经更新到ACF PRO v5。2.3

有人知道修复方法吗?

下面是在WP v3上工作得很好的代码。9

我的活动Widge模板:

<?php
/* Template Name: Events Widget */
$today = date(\'Ymd\');

if (isset($_GET[\'_m\'])) {

    $current_month = str_pad($_GET[\'_m\'], 2, \'0\', STR_PAD_LEFT);
    $current_day = "01"; // day one
    $current_year = $_GET[\'_y\'];

    $get_last_day = $current_year.$current_month.$current_day;
    $lastday = date("t", strtotime($get_last_day));

    $tempstartday = $current_year.$current_month.$current_day;
    $tempendday = $current_year.$current_month.$lastday;

    $startday = date(\'Ymd\', strtotime($tempstartday));
    $endday = date(\'Ymd\', strtotime($tempendday));

} else {

    $current_month = str_pad(date(\'m\'), 2, \'0\', STR_PAD_LEFT);
    $current_day = "01"; // day one
    $current_year = date(\'Y\');

    $get_last_day = $current_year.$current_month.$current_day;
    $lastday = date("t", strtotime($get_last_day));

    $tempstartday = $current_year.$current_month.$current_day;
    $tempendday = $current_year.$current_month.$lastday;

    $startday = date(\'Ymd\', strtotime($tempstartday));
    $endday = date(\'Ymd\', strtotime($tempendday));
}

add_filter( \'get_meta_sql\', \'get_meta_sql_date\', 10, 2 );

$qryevents = array(
    \'post_type\' => \'events\',
    \'posts_per_page\' => 50,
    \'status\' => \'published\',
    \'meta_key\' => \'event_start_date\',
    \'orderby\' => \'meta_value\',
    \'order\' => \'ASC\',

    // produces meta join and where clauses for the query
    // which will be filtered in functions.php
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'event_start_date\',
            \'compare\' => \'>=\',
            \'value\'   => $startday,
            \'type\' => \'DATE\'
        ),
        array(
            \'key\'     => \'event_end_date\',
            \'compare\' => \'<=\',
            \'value\'   => $endday,
            \'type\' => \'DATE\'
        )
    )
);

$loop = new WP_Query( $qryevents );
remove_filter( \'get_meta_sql\', \'get_meta_sql_date\', 10, 2 );

if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();

// Let\'s format the dates
$get_start_date = get_field(\'event_start_date\');
$get_end_date = get_field(\'event_end_date\');
$event_start_date = DateTime::createFromFormat(\'Ymd\', $get_start_date);
$event_end_date = DateTime::createFromFormat(\'Ymd\', $get_end_date);
// End of date definitions

// Let\'s get the event start and end times
$get_start_time = get_field(\'event_start_time\');
$get_end_time = get_field(\'event_end_time\');
// end of times

// Let\'s get the times of the events now
$specify_event_time = "";

$show_event_times = get_field(\'specify_event_times\');
if($show_event_times){
        foreach($show_event_times as $specify_event_time){
            // Do nothing; this puts the yes value into the varible for us to later on the page.
            // echo $specify_event_time;
        }
}
// End of the specify times


$event_month_spans = get_field(\'event_month_span\');
?>

<div class="<?php echo (++$j % 2 == 0) ? \'full row\' : \'full row alt\'; ?>">
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    <?php if($specify_event_time == "yes"): ?>
    <p class="event-date"><a href="<?php the_permalink(); ?>"><?php echo $event_start_date->format(\'M d, Y\'); ?> <?php echo $get_start_time; ?> - <?php echo $event_end_date->format(\'M d, Y\'); ?> <?php echo $get_end_time; ?></a></p>
  <?php else: ?>
    <p class="event-date"><a href="<?php the_permalink(); ?>"><?php echo $event_start_date->format(\'M d, Y\'); ?> - <?php echo $event_end_date->format(\'M d, Y\'); ?></a></p>
  <?php endif; ?>
</div>   

<?php
endwhile;

else:
?>
<p>No scheduled events.</p>
<?php endif; wp_reset_query(); ?>
在我的功能中。php文件我有以下功能:

function get_meta_sql_date( $pieces, $queries ) {
    global $wpdb;

    // get start and end date from query
    foreach ( $queries as $q ) {

        if ( !isset( $q[\'key\'] ) ) {
            return $pieces;
        }

        if ( \'event_start_date\' === $q[\'key\'] ) {
            $start_date = isset( $q[\'value\'] ) ?  $q[\'value\'] : \'\';
        }
        if ( \'event_end_date\' === $q[\'key\'] ) {
            $end_date = isset( $q[\'value\'] ) ?  $q[\'value\'] : \'\';
        }
    }

    if ( ( \'\' === $start_date ) || ( \'\' === $end_date ) ) {
        return $pieces;
    }

    $query = "";

    // after start date AND before end date
    $_query = " AND (
        ( $wpdb->postmeta.meta_key = \'event_start_date\' AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s) )
        AND ( mt1.meta_key = \'event_end_date\' AND ( CAST(mt1.meta_value AS DATE) <= %s) )
    )";
    $query .= $wpdb->prepare( $_query, $start_date, $end_date );

    // OR before start date AND after end date
    $_query = " OR (
        ( $wpdb->postmeta.meta_key = \'event_start_date\' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) )
        AND ( mt1.meta_key = \'event_end_date\' AND ( CAST(mt1.meta_value AS DATE) >= %s) )
    )";
    $query .= $wpdb->prepare( $_query, $start_date, $end_date );

    // OR before start date AND (before end date AND end date after start date)
    $_query = " OR (
        ( $wpdb->postmeta.meta_key = \'event_start_date\' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) )
        AND ( mt1.meta_key = \'event_end_date\'
            AND ( CAST(mt1.meta_value AS DATE) <= %s )
            AND ( CAST(mt1.meta_value AS DATE) >= %s )
        )
    )";
    $query .= $wpdb->prepare( $_query, $start_date, $end_date, $start_date );

    // OR after end date AND (after start date AND start date before end date) )
    $_query = "OR (
        ( mt1.meta_key = \'event_end_date\' AND ( CAST(mt1.meta_value AS DATE) >= %s ) )
        AND ( $wpdb->postmeta.meta_key = \'event_start_date\'
            AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s )
            AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s )
        )
    )";
    $query .= $wpdb->prepare( $_query, $end_date, $start_date, $end_date );

    $pieces[\'where\'] = $query;

    return $pieces;
}

2 个回复
SO网友:TheDeadMedic

不需要定制的meta-SQL过滤器——以这种格式存储日期的美妙之处Ymd 您可以对它们进行数字处理,MySQL仍然能够找到给定“范围”内的事件,并对它们进行升序/降序排序。

最近,我在另一个网站上使用ACF作为开始/结束日期进行了此操作:

if ( ! empty( $_GET[\'_y\'] ) )
    $year = absint( $_GET[\'_y\'] );
else
    $year = date( \'Y \');

if ( ! empty( $_GET[\'_m\'] ) && in_array( $month = absint( $_GET[\'_m\'] ), range( 1, 12 ) ) )
    $month = zeroise( $month, 2 );
else
    $month = date( \'m\' );

$qryevents = array(
    \'post_type\'      => \'events\',
    \'posts_per_page\' => 50,
    \'post_status\'    => \'publish\',
    \'orderby\'        => \'meta_value_num\', // Ensure order is numerically based
    \'order\'          => \'ASC\',

    \'meta_query\'     => array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'event_start_date\',
            \'compare\' => \'>=\',
            \'value\'   => "{$year}{$month}01",
            \'type\'    => \'NUMERIC\',
        ),
        array(
            \'key\'     => \'event_end_date\',
            \'compare\' => \'<=\',
            \'value\'   => "{$year}{$month}31", // Doesn\'t matter if there aren\'t 31 days in this month, will still work,
            \'type\'    => \'NUMERIC\',
        )
    )
);
不需要过于复杂的日期字符串计算,也不需要过滤器。

SO网友:Swati Sharma

日期(&A);时间实用程序使用日期和;时间模式。这个date () 实用程序重新访问此模式中的日期:“YYYY-MM-DD”。

SQL Date functions oracle

结束
在WordPress 4.2.1中使用日期选择器筛选分隔符在日期之间进行查询 - 小码农CODE - 行之有效找到问题解决它

在WordPress 4.2.1中使用日期选择器筛选分隔符在日期之间进行查询

时间:2015-05-01 作者:Robbiegod

原职务:Query between dates using Date Picker fields

我最初在去年发表了一篇关于同一主题的帖子,但似乎是对WP v4的更新。2.1坏了我的过滤器。总之,我正在事件自定义帖子类型中使用ACF和日期选择器插件。我正在尝试查询开始日期和结束日期之间的事件。我也有可以跨越多个月的活动。例如,一项活动可能在4月开始,但在6月结束。我希望该活动在4月、5月和6月举行。

在v3上,我使用过滤器实现了这一点。wordpress的第9页。我正在尝试更新我所有网站上的wordpress,但此过滤器在更新到v4后会中断。2.1。在更新WP 4.2.1和将ACF更新到最新版本4之后,这一问题就解决了。我已经更新到ACF PRO v5。2.3

有人知道修复方法吗?

下面是在WP v3上工作得很好的代码。9

我的活动Widge模板:

<?php
/* Template Name: Events Widget */
$today = date(\'Ymd\');

if (isset($_GET[\'_m\'])) {

    $current_month = str_pad($_GET[\'_m\'], 2, \'0\', STR_PAD_LEFT);
    $current_day = "01"; // day one
    $current_year = $_GET[\'_y\'];

    $get_last_day = $current_year.$current_month.$current_day;
    $lastday = date("t", strtotime($get_last_day));

    $tempstartday = $current_year.$current_month.$current_day;
    $tempendday = $current_year.$current_month.$lastday;

    $startday = date(\'Ymd\', strtotime($tempstartday));
    $endday = date(\'Ymd\', strtotime($tempendday));

} else {

    $current_month = str_pad(date(\'m\'), 2, \'0\', STR_PAD_LEFT);
    $current_day = "01"; // day one
    $current_year = date(\'Y\');

    $get_last_day = $current_year.$current_month.$current_day;
    $lastday = date("t", strtotime($get_last_day));

    $tempstartday = $current_year.$current_month.$current_day;
    $tempendday = $current_year.$current_month.$lastday;

    $startday = date(\'Ymd\', strtotime($tempstartday));
    $endday = date(\'Ymd\', strtotime($tempendday));
}

add_filter( \'get_meta_sql\', \'get_meta_sql_date\', 10, 2 );

$qryevents = array(
    \'post_type\' => \'events\',
    \'posts_per_page\' => 50,
    \'status\' => \'published\',
    \'meta_key\' => \'event_start_date\',
    \'orderby\' => \'meta_value\',
    \'order\' => \'ASC\',

    // produces meta join and where clauses for the query
    // which will be filtered in functions.php
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'event_start_date\',
            \'compare\' => \'>=\',
            \'value\'   => $startday,
            \'type\' => \'DATE\'
        ),
        array(
            \'key\'     => \'event_end_date\',
            \'compare\' => \'<=\',
            \'value\'   => $endday,
            \'type\' => \'DATE\'
        )
    )
);

$loop = new WP_Query( $qryevents );
remove_filter( \'get_meta_sql\', \'get_meta_sql_date\', 10, 2 );

if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();

// Let\'s format the dates
$get_start_date = get_field(\'event_start_date\');
$get_end_date = get_field(\'event_end_date\');
$event_start_date = DateTime::createFromFormat(\'Ymd\', $get_start_date);
$event_end_date = DateTime::createFromFormat(\'Ymd\', $get_end_date);
// End of date definitions

// Let\'s get the event start and end times
$get_start_time = get_field(\'event_start_time\');
$get_end_time = get_field(\'event_end_time\');
// end of times

// Let\'s get the times of the events now
$specify_event_time = "";

$show_event_times = get_field(\'specify_event_times\');
if($show_event_times){
        foreach($show_event_times as $specify_event_time){
            // Do nothing; this puts the yes value into the varible for us to later on the page.
            // echo $specify_event_time;
        }
}
// End of the specify times


$event_month_spans = get_field(\'event_month_span\');
?>

<div class="<?php echo (++$j % 2 == 0) ? \'full row\' : \'full row alt\'; ?>">
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    <?php if($specify_event_time == "yes"): ?>
    <p class="event-date"><a href="<?php the_permalink(); ?>"><?php echo $event_start_date->format(\'M d, Y\'); ?> <?php echo $get_start_time; ?> - <?php echo $event_end_date->format(\'M d, Y\'); ?> <?php echo $get_end_time; ?></a></p>
  <?php else: ?>
    <p class="event-date"><a href="<?php the_permalink(); ?>"><?php echo $event_start_date->format(\'M d, Y\'); ?> - <?php echo $event_end_date->format(\'M d, Y\'); ?></a></p>
  <?php endif; ?>
</div>   

<?php
endwhile;

else:
?>
<p>No scheduled events.</p>
<?php endif; wp_reset_query(); ?>
在我的功能中。php文件我有以下功能:

function get_meta_sql_date( $pieces, $queries ) {
    global $wpdb;

    // get start and end date from query
    foreach ( $queries as $q ) {

        if ( !isset( $q[\'key\'] ) ) {
            return $pieces;
        }

        if ( \'event_start_date\' === $q[\'key\'] ) {
            $start_date = isset( $q[\'value\'] ) ?  $q[\'value\'] : \'\';
        }
        if ( \'event_end_date\' === $q[\'key\'] ) {
            $end_date = isset( $q[\'value\'] ) ?  $q[\'value\'] : \'\';
        }
    }

    if ( ( \'\' === $start_date ) || ( \'\' === $end_date ) ) {
        return $pieces;
    }

    $query = "";

    // after start date AND before end date
    $_query = " AND (
        ( $wpdb->postmeta.meta_key = \'event_start_date\' AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s) )
        AND ( mt1.meta_key = \'event_end_date\' AND ( CAST(mt1.meta_value AS DATE) <= %s) )
    )";
    $query .= $wpdb->prepare( $_query, $start_date, $end_date );

    // OR before start date AND after end date
    $_query = " OR (
        ( $wpdb->postmeta.meta_key = \'event_start_date\' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) )
        AND ( mt1.meta_key = \'event_end_date\' AND ( CAST(mt1.meta_value AS DATE) >= %s) )
    )";
    $query .= $wpdb->prepare( $_query, $start_date, $end_date );

    // OR before start date AND (before end date AND end date after start date)
    $_query = " OR (
        ( $wpdb->postmeta.meta_key = \'event_start_date\' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) )
        AND ( mt1.meta_key = \'event_end_date\'
            AND ( CAST(mt1.meta_value AS DATE) <= %s )
            AND ( CAST(mt1.meta_value AS DATE) >= %s )
        )
    )";
    $query .= $wpdb->prepare( $_query, $start_date, $end_date, $start_date );

    // OR after end date AND (after start date AND start date before end date) )
    $_query = "OR (
        ( mt1.meta_key = \'event_end_date\' AND ( CAST(mt1.meta_value AS DATE) >= %s ) )
        AND ( $wpdb->postmeta.meta_key = \'event_start_date\'
            AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s )
            AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s )
        )
    )";
    $query .= $wpdb->prepare( $_query, $end_date, $start_date, $end_date );

    $pieces[\'where\'] = $query;

    return $pieces;
}

2 个回复
SO网友:TheDeadMedic

不需要定制的meta-SQL过滤器——以这种格式存储日期的美妙之处Ymd 您可以对它们进行数字处理,MySQL仍然能够找到给定“范围”内的事件,并对它们进行升序/降序排序。

最近,我在另一个网站上使用ACF作为开始/结束日期进行了此操作:

if ( ! empty( $_GET[\'_y\'] ) )
    $year = absint( $_GET[\'_y\'] );
else
    $year = date( \'Y \');

if ( ! empty( $_GET[\'_m\'] ) && in_array( $month = absint( $_GET[\'_m\'] ), range( 1, 12 ) ) )
    $month = zeroise( $month, 2 );
else
    $month = date( \'m\' );

$qryevents = array(
    \'post_type\'      => \'events\',
    \'posts_per_page\' => 50,
    \'post_status\'    => \'publish\',
    \'orderby\'        => \'meta_value_num\', // Ensure order is numerically based
    \'order\'          => \'ASC\',

    \'meta_query\'     => array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'event_start_date\',
            \'compare\' => \'>=\',
            \'value\'   => "{$year}{$month}01",
            \'type\'    => \'NUMERIC\',
        ),
        array(
            \'key\'     => \'event_end_date\',
            \'compare\' => \'<=\',
            \'value\'   => "{$year}{$month}31", // Doesn\'t matter if there aren\'t 31 days in this month, will still work,
            \'type\'    => \'NUMERIC\',
        )
    )
);
不需要过于复杂的日期字符串计算,也不需要过滤器。

SO网友:Swati Sharma

日期(&A);时间实用程序使用日期和;时间模式。这个date () 实用程序重新访问此模式中的日期:“YYYY-MM-DD”。

SQL Date functions oracle

相关推荐

如何通过jQuery Datepicker搜索帖子?

我正在WordPress网站上工作,我有自己的搜索筛选页面,在那里我添加了jquery日期选择器,现在我想按日期选择器搜索帖子?Html Codescript> $(function() { jQuery( \"#datepicker-13\" ).datepicker({ dateFormat : \"yy-mm-dd\" }); $(\"#datepicker-13\").datepicker().datepick