0

I want to create a shortcode to display the posts posted on actual day.

I found this code: show number of posts posted today And I'm trying with it, but return 0

function ts_day_f() {
// we get the date for today
$today = getdate();
//we set the variables, i am ignoring sticky posts so they dont get counted
$args = array(
    'ignore_sticky_posts' => 1,
    'posts_per_page' => -1, //all posts 
    'date_query' => array(
        array(
            'year'  => $today["year"],
            'month' => $today["mon"],
            'day'   => $today["mday"],
        ),
    ),
);
//we create the query
$today_posts = new WP_Query( $args );
//the result already has a property with the number of posts returned
$count = $today_posts->post_count;
//show it
return $count;
}

add_shortcode('ts_day','ts_day_f');

Any idea?

2
  • So, where is a problem if your shortocode works?
    – anton
    Jun 19 at 7:26
  • Now nothing, I made it work after several tries
    – Velask
    Jun 19 at 19:06
0

I tried some codes, and I think finally works after several tries:

function ts_day_f() {
    $posts= get_posts(array(
            'numberposts' => -1,
            'post_status' => 'publish',
            'orderby' => 'date',
            'order'   => 'DESC',
            'date_query'    => array(
                'column'  => 'post_date',
                'after'     => 'today',
                'before'    => 'tomorrow - 1 second',
            )
        ));

    if ( is_user_logged_in()) {
        //return count($posts);
        $c = count($posts);

        return "Today: <span style=\"font-size: 1.5em;\">" .$c. "</span> posts.";
    }
    else  {
        return $null;
    }
}

add_shortcode('ts_day','ts_day_f');

Additionally with the function to show only to users log in.

2
  • Accept your answer as correct, please.
    – anton
    Jun 19 at 20:00
  • done, I did, thank you
    – Velask
    Jun 21 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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