用于在网站上显示时间发布内容的自定义WordPress发布查询

时间:2020-01-07 作者:Mike Hermary

我正在尝试使用WordPress查询实现一个“内容点播风格/时间发布内容”订阅系统,该查询使用设置的参数从自定义帖子类型中过滤和选择内容,并将其显示在页面模板上。下面包括解释、一些示例、查询的初始参数以及零售商(页面)和帖子的结构。

这是否可能通过WP查询实现,或者是否需要插件?我已经研究过会员插件,但对于这种定制级别来说,它们似乎太过分了。

<?php
/**
 * Pages that are defined as \'Retailers\'.
 * Each retailer (page) will have a defined start date using an Advanced Custom Fields date picker field. This field is set manually by the site admin.

 * The retailer pages will display a defined number of posts from the \'sendouts\' custom post type in a HTML dropdown (explained below).
 * The number of displayed posts is handled in part by the start date parameter.
 * A new post will be added every 14 days (2 weeks).

 * EXAMPLE 1: Retailer A has a start date of January 1, 2019. Counting up from that start date to the current date (January 7, 2020),
 * the retailer will have a total of 26 posts accessible from the HTML dropdown.

 * EXAMPLE 2: Retailer B has a start date of December 1, 2019. Counting up from that start date to the current date (January 7, 2020,
 * the retailer will have a total 2 posts accessible from the HTML dropdown.

 * EXAMPLE 3: Retailer C has a start date of January 1, 2020. As the start date is less than 14 days (two weeks) old, only one post will be displayed
 * and the HTML dropdown will not be displayed on the page.
 * 
 */

// Advanced Custom Fields date picker field assigned to each Retailer page
$startDate = get_field( \'sendout_start_date\' );

// Set the timezone to America/Edmonton to localize date to region
date_default_timezone_set( \'America/Edmonton\' );

// Grab the current date for the set timezone
$currentDate = date( \'m/d/Y h:i:s a\', time() );

// Add fourteen days to the current date
// Use this variable in a loop to increment the total number of visible posts by one every 14 days
$datePlusFourteen = date_add( $currentDate, new DateInterval( \'P14D\' ));

$args = array(
    \'post_type\' => \'sendouts\',
    \'cat\' => \'101\',
    \'order\' => \'ASC\',
    \'orderby\' => \'p\',
    \'meta_query\' => array(
        array(
            \'key\' => \'sendout_start_date\',
            \'type\' => \'NUMERIC\',
            \'value\' => array( \'sendout_start_date\', $currentDate ),
            \'compare\' => \'BETWEEN\',
        ),
    ),
);
$query = new WP_Query( $args );

Update

页面发送开始日期:2019年10月18日

发送后日期:

1月7日,12月1日,11月15日,11月1日,10月15日

1 个回复
SO网友:TheDeadMedic

使用meta query. 因为ACF将日期存储为Ymd 您可以将该值视为一个数字:

$date_query = array(
    \'key\' => \'start_date\', // ACF date field name
    \'type\' => \'NUMERIC\',
    \'value\' => \'20200101\',
    \'compare\' => \'<=\', // All posts with start date before/on January 1st 2020
);

$args[\'meta_query\'] = array( $date_query );
任何典型比较,例如。=, !=, >, >=, <, <= 可用于的值compare.

或者,您需要一个日期范围,例如,要获得2019年12月的所有帖子:

$date_query = array(
    \'key\' => \'start_date\',
    \'type\' => \'NUMERIC\',
    \'value\' => array( \'20191201\' /* Start date */, \'20191231\' /* End date */ ),
    \'compare\' => \'BETWEEN\',
);
下面是一个(未经测试的)问题示例:

// Grab the current date for the set timezone
$currentDate = new DateTimeImmutable( current_time( \'mysql\', true /* GMT */ ), new DateTimeZone( \'America/Edmonton\' ) );

// Add fourteen days to the current date
// Use this variable in a loop to increment the total number of visible posts by one every 14 days
$datePlusFourteen = $currentDate->add( new DateInterval( \'P14D\' ) );

$args = array(
    \'post_type\' => \'sendouts\',
    \'cat\' => \'101\',
    \'order\' => \'ASC\',
    \'orderby\' => \'p\',
    \'meta_query\' => array(
        array(
            \'key\' => \'sendout_start_date\',
            \'type\' => \'NUMERIC\',
            \'value\' => array( $currentDate->format( \'Ymd\' ), $datePlusFourteen->format( \'Ymd\' ) ),
            \'compare\' => \'BETWEEN\',
        ),
    ),
);

$query = new WP_Query( $args );

相关推荐

Div-Wrap with Functions.php in ChildTheme Using Shorcode!

我只想为一个简单样式的DIV包装器创建一个短代码。在WordPress的网站上,我想添加如下内容:[quotehead]Headline text[/quotehead] ...a normal blockquote from wordpress... 输出应为:<div class=\"block_header\">Headline text</div> 我的内部功能functions.php (在childtheme中)包含以下内容:/**