admin管理员组

文章数量:1406944

this is my code, and the result shows always the total number of comments instead of number of comments after 12 february 2020.

I don't know why it does not working.

    #!/usr/bin/php

    <?php
    if ( ! defined('ABSPATH') ) {
        /** Set up WordPress environment */
        require_once( dirname( __FILE__ ) . '/wp-load.php' );
    }

    $args1 = array(
            'status' => 'approve',
            'date_query' => array(
                array (
                   'after' => '2020-02-12 10:00:00',
                ),
             ),
          );

    $comments = new WP_Comment_Query( $args1 );

    $comms = get_comments( $comments );

    $nbr = count( $comms );


    echo "$nbr"
    ?>

thank you for your help

this is my code, and the result shows always the total number of comments instead of number of comments after 12 february 2020.

I don't know why it does not working.

    #!/usr/bin/php

    <?php
    if ( ! defined('ABSPATH') ) {
        /** Set up WordPress environment */
        require_once( dirname( __FILE__ ) . '/wp-load.php' );
    }

    $args1 = array(
            'status' => 'approve',
            'date_query' => array(
                array (
                   'after' => '2020-02-12 10:00:00',
                ),
             ),
          );

    $comments = new WP_Comment_Query( $args1 );

    $comms = get_comments( $comments );

    $nbr = count( $comms );


    echo "$nbr"
    ?>

thank you for your help

Share Improve this question edited Feb 26, 2020 at 11:49 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Feb 26, 2020 at 11:40 Njaka Eric RavoavyNjaka Eric Ravoavy 11 bronze badge 5
  • I think your issue is calling get_comments(). Once you've done your initial WP_Comment_Query() you should have just the comments you need, in an array. Try a var_dump($comments) to see. If you're then passing those comments into get_comments() it's likely get_comments() isn't recognizing them as an argument and so you get all comments instead. – WebElaine Commented Feb 26, 2020 at 15:24
  • Hi WebElain thank you very much for your help. You are right, the var_dump($comments) query shows the right result of comments starting at my query date. Now, my question is how can I do to count that number of comments result ? – Njaka Eric Ravoavy Commented Feb 26, 2020 at 18:32
  • Just use count($comments) – WebElaine Commented Feb 26, 2020 at 20:07
  • Thank you, yes I tried that but it return "1" while comments is not just one. :-( – Njaka Eric Ravoavy Commented Feb 26, 2020 at 21:29
  • Your var_dump should tell you what type of item it is (perhaps it's an object or a nested array) so you can get at the number of comments. – WebElaine Commented Feb 26, 2020 at 23:05
Add a comment  | 

1 Answer 1

Reset to default 0

thank you for WebElain to help me. This answer driving me to the right request.

Now it's ok. It is an object indead and I use something more simple with get_comments () query with parameter count enabled.

the final code is like this :

#!/usr/bin/php

<?php
if ( ! defined('ABSPATH') ) {
    /** Set up WordPress environment */
    require_once( dirname( __FILE__ ). '/wordpress/wp-load.php' );
}

$output = shell_exec('date --date="10 day ago" "+%Y-%m-%d %T"');

echo "$output";

// WP_Comment_Query arguments

$args1 = array(
        'status' => 'approve',
        'count' => true,
                'date_query' => array(
                    array (
                       'after' => $output,
                    ),
                 ),
              );

//$comms = var_dump( $comments );

$nbr = get_comments( $args1 );

echo "$nbr\n";

?>

Thank you.

本文标签: I would like to count number of comments after 5 days ago