按当前日期筛选帖子(WP帖子旋转传送带)

时间:2016-07-08 作者:Luarb Balla

所以我设法用这个片段过滤帖子:

<?php 
echo " works";


$current_day = date(\'j\');
$current_month = date(\'M\');
query_posts(\'day=\'.$current_day.\'&month=\'.$current_month);
if (have_posts()):
    while (have_posts()) : the_post();



    endwhile;
endif;

?>
问题是,该插件使用其他方式来查询帖子,确切地说:

 require_once ( "includes/wp-posts-carousel-popular-posts-query.class.php" );
            $loop = new WP_Posts_Carousel_Popular_Posts_Query( apply_filters(\'wpc_query\', $query_args, array(
                \'params\' => $params,
            ) ) );
        } else {
            $loop = new WP_Query( apply_filters(\'wpc_query\', $query_args, array( 
                \'params\' => $params,
            ) ) );
        }
wp posts carousel热门帖子查询。班php:

<?php
/*
Author: Marcin Gierada
Author URI: http://www.teastudio.pl/
Author Email: [email protected]
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

if ( ! defined( \'ABSPATH\' ) ) {
    exit; // Exit if accessed directly
}

/*
 * class to get populat posts from Wordpress Popular Posts plugin
 */
class WP_Posts_Carousel_Popular_Posts_Query extends WP_Query {
    private $args;

    function __construct($args) {
        $this->args = $args;

        add_filter( \'posts_fields\', array( $this, \'posts_fields\') );
        add_filter( \'posts_orderby\', array( $this, \'posts_orderby\' ) );

        parent::__construct($args);

        remove_filter( \'posts_fields\', array( $this, \'posts_fields\' ) );
        remove_filter( \'posts_orderby\', array( $this, \'posts_orderby\' ) );
    }

    function posts_fields($sql) {
        global $wpdb;
        return $sql . ", (SELECT SUM(". $wpdb->prefix . "popularpostsdata.pageviews) FROM " . $wpdb->prefix . "popularpostsdata WHERE postid=" . $wpdb->posts . ".ID AND last_viewed > DATE_SUB(\'" . current_time(\'mysql\') . "\', INTERVAL 1 MONTH)) AS views";
    }

    function posts_orderby($sql) {
        return "views " . $this->args[\'order\'];
    }
}
?>
有人能帮我转换代码片段并将其功能与插件功能合并吗?

1 个回复
最合适的回答,由SO网友:knif3r 整理而成

您可以使用WP_Query 要查询从日期A到日期B的帖子,以下是WP_Query 对象:

//////Date Parameters - Show posts associated with a certain time and date period.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
    \'year\' => 2014,                         //(int) - 4 digit year (e.g. 2011).
    \'monthnum\' => 4,                        //(int) - Month number (from 1 to 12).
    \'w\' =>  25,                             //(int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependenon the "start_of_week" option.
    \'day\' => 17,                            //(int) - Day of the month (from 1 to 31).
    \'hour\' => 13,                           //(int) - Hour (from 0 to 23).
    \'minute\' => 19,                         //(int) - Minute (from 0 to 60).
    \'second\' => 30,                         //(int) - Second (0 to 60).
    \'m\' => 201404,                          //(int) - YearMonth (For e.g.: 201307).
    \'date_query\' => array(                  //(array) - Date parameters (available with Version 3.7).
                                              //these are super powerful. check out the codex for more comprehensive code examples http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
      array(
        \'year\' => 2014,                     //(int) - 4 digit year (e.g. 2011).
        \'month\' => 4                        //(int) - Month number (from 1 to 12).
        \'week\' => 31                        //(int) - Week of the year (from 0 to 53).
        \'day\' => 5                          //(int) - Day of the month (from 1 to 31).
        \'hour\' => 2                         //(int) - Hour (from 0 to 23).
        \'minute\' => 3                       //(int) - Minute (from 0 to 59).
        \'second\' => 36                      //(int) - Second (0 to 59).
        \'after\'     => \'January 1st, 2013\', //(string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string, or array of \'year\', \'month\', \'day\'
        \'before\'    => array(               //(string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string, or array of \'year\', \'month\', \'day\'
          \'year\'  => 2013,                  //(string) Accepts any four-digit year. Default is empty.
          \'month\' => 2,                     //(string) The month of the year. Accepts numbers 1-12. Default: 12.
          \'day\'   => 28,                    //(string) The day of the month. Accepts numbers 1-31. Default: last day of month.
        ),
        \'inclusive\' => true,                //(boolean) - For after/before, whether exact value should be matched or not\'.
        \'compare\' =>  \'=\',                  //(string) - Possible values are \'=\', \'!=\', \'>\', \'>=\', \'<\', \'<=\', \'LIKE\', \'NOT LIKE\', \'IN\', \'NOT IN\', \'BETWEEN\', \'NOT BETWEEN\', \'EXISTS\' (only in WP >= 3.5), and \'NOT EXISTS\' (also only in WP >= 3.5). Default value is \'=\'
        \'column\' => \'post_date\',            //(string) - Column to query against. Default: \'post_date\'.
        \'relation\' => \'AND\',                //(string) - OR or AND, how the sub-arrays should be compared. Default: AND.
      ),
    ),
资料来源:https://gist.github.com/luetkemj/2023628

相关推荐

WPQuery Date and ACF

我正在创建一个查询,需要按ACF日期字段进行筛选。在数据库中(wp\\u post\\u meta table中)的值如下:MM/DD/YYYY我有几个房间,在date\\u available\\u from字段中的值为:2021 06月01日2021 08月21日2021 08月31日2021 09月06日2021 07月30日2021 11月31日2021 12月31日ul>然后我像这样进行查询:$args[1] = array( \'posts_per_page\' => -1