0

So i managed to filter the posts with this snippet :

<?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;

?>

The thing is that this plugin uses some other way to query the posts , exactly :

 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-popular-posts-query.class.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'];
    }
}
?>

Can someone help me to convert my snippet and merge its function with the plugins functions ?

1 Answer 1

0

You can use WP_Query to query posts from Date A to Date B, here are all Date Parameters for the WP_Query object :

//////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.
      ),
    ),

Source : https://gist.github.com/luetkemj/2023628

1
  • Also you can get the current timestamp by simply putting time() in a variable.
    – knif3r
    Jul 8, 2016 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.