Skip Navigation

[Resolved] Something strange happening with a count of posts

This thread is resolved. Here is a description of the problem and solution.

Problem:
Filter the post view by publish date, which is setup by Views shortcode attribute.

Solution:
Views is using WordPress class WP_Query to query the post, see the document:
http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
The days parameter of Views is correspond to "The day of the month", but in your case, it needs a strtotime()-compatible string, for example:
after 30 days ago

So it needs custom codes with Views filter hook to apply such a filter, I added below codes into your theme/functions.php:

// https://toolset.com/forums/topic/something-strange-happening-with-a-count-of-posts/
add_filter( 'wpv_filter_query', 'my_date_filter_func', 99, 3 );
 
function my_date_filter_func( $query_args, $settings, $views_id ) {
    if ( $views_id == 1555 ) {
        if(isset($query_args['date_query'][0]['day'])){
            $query_args['date_query'][0] = array(
                'after' => $query_args['date_query'][0]['day'] . ' days ago',
                'column' => 'post_date_gmt',
            );
        }
    }
    return $query_args;
}

Relevant Documentation:
http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

This support ticket is created 8 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by gavinS 8 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#361001
new jobs.png

I have something strange happening in one of my views.

I am doing a count of posts with certain filters for the past 7 days, and then for the past 30 days, so 30 days should always be greater than or equal to the 7 days total, but in my view it is coming up as less than?

These are the two count views:

View: bm-new-jobs-7-days

Selects 'jobs' with the following filters:

Post author filter
Select posts with the author set by the parent view

Post date filter
Select posts whose
Published date is after or equal to: day:PAST_ONE(7)

Loop output:


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
    [wpv-found-count]
	[/wpv-items-found]
	[wpv-no-items-found]
		[wpml-string context="wpv-views"]0[/wpml-string]
	[/wpv-no-items-found]
[wpv-layout-end]

View: bm-new-jobs-30-days

Selects 'jobs' with the following filters:

Post author filter
Select posts with the author set by the parent View.

Post date filter
Select posts whose
Published date is after or equal to: day:PAST_ONE(30)

Loop output:


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
    [wpv-found-count]
	[/wpv-items-found]
	[wpv-no-items-found]
		[wpml-string context="wpv-views"]0[/wpml-string]
	[/wpv-no-items-found]
[wpv-layout-end]

And this is the output for the other view which calls these two views:

Selects users with any role, no filters


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          
          [wpv-conditional if="('[types usermeta="location"][/types]' eq check_user_from_same_location())"]
          <div class = "row"> <strong>[wpv-user field="display_name"]</strong></div><br>
          <div class = "row">
            <p>Last 7 days</p>
          </div>
          <div class = "row">
             <div class = "col-xs-4 col-md-2">
            <a href = "<em><u>hidden link</u></em>">Emails:</a> [wpv-view name="bm-comms-7-days" toc = "1"]</div>
          <div class = "col-xs-4 col-md-2">
           <a href = "<em><u>hidden link</u></em>">Phone:</a> [wpv-view name="bm-comms-7-days" toc = "2"]</div>
            <div class = "col-xs-4 col-md-2">
              <a href = "<em><u>hidden link</u></em>">Meetings:</a> [wpv-view name="bm-comms-7-days" toc = "3"]</div></div><br>
          
          <div class = "row">
            <div class = "col-xs-4 col-md-2">
                 New Jobs:
               [wpv-view name="bm-new-jobs-7-days"]</div>
            <div class = "col-xs-4 col-md-2">
                 CVs Sent:
               [wpv-view name="bm-cvs-sent-7-days"]</div>
            <div class = "col-xs-4 col-md-2">
                 Lost Jobs:
               [wpv-view name="bm-lost-jobs-7-days"]</div>
            <div class = "col-xs-4 col-md-2">
                 1st Interview:
               </div>
            <div class = "col-xs-4 col-md-2">
                 2nd Interview:
               </div>
            <div class = "col-xs-4 col-md-2">
                 Placements:
               </div>
          </div><br>
          <div class = "row"> 
          <p>Last 30 days</p>
            </div>
          <div class = "row">
           <div class = "col-xs-4 col-md-2">
          
          <a href = "<em><u>hidden link</u></em>">Emails:</a> [wpv-view name="bm-comms-30-days" toc = "1"]</div>
          <div class = "col-xs-4 col-md-2">
            <a href = "<em><u>hidden link</u></em>">Phone:</a> [wpv-view name="bm-comms-30-days" toc = "2"]</div>
            <div class = "col-xs-4 col-md-2">
              <a href = "<em><u>hidden link</u></em>">Meetings:</a> [wpv-view name="bm-comms-30-days" toc = "3"]</div></div><br>
          <div class = "row">
            <div class = "col-xs-4 col-md-2">
                 New Jobs:
               [wpv-view name="bm-new-jobs-30-days"]</div>
            <div class = "col-xs-4 col-md-2">
                 CVs Sent:
               [wpv-view name="bm-cvs-sent-30-days"]</div>
            <div class = "col-xs-4 col-md-2">
                 Lost Jobs:
              [wpv-view name="bm-lost-jobs-30-days"]</div>
            <div class = "col-xs-4 col-md-2">
                 1st Interview:
               </div>
            <div class = "col-xs-4 col-md-2">
                 2nd Interview:
               </div>
            <div class = "col-xs-4 col-md-2">
                 Placements:
               </div>
          </div><br>
          [/wpv-conditional]
         
		</wpv-loop>

	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

This calls the following function to only display the users in the same location as the logged in user:


function check_user_from_same_location($type, $object) {
    if ( $type == 'users' ) {
        if ( empty( $object) ) {
            $return = 0;
        } else {
            $current_user = wp_get_current_user();
            $user_id = $current_user->ID;
            $user_location = get_user_meta($user_id, 'wpcf-location', true);
            switch ($user_location) {
                case 1:
                    $return = 'Durban'; 
                    break;
                case 2:
                    $return = 'Joburg'; 
                    break;
                case 3:
                    $return = 'Cape Town'; 
                    break;
                case 4:
		    $return = 'Pietermaritzburg';
                    break;
                case 5:
                    $return = 'Head Office';
                    break;
            }
        }
    }
    return $return;
}

As you can see from the attached screenshot the count for 7 days for new jobs is 5, but the count for 30 days is zero? I don't understand?

#361191

Dear gavin,

Your case is complicate, could you duplicate same problem in a test site, and fill below private detail box with login details, also point out the problem page URL, I need a live website to test and debug this problem, thanks

#361230

Thanks for the details, I have been able to duplicate same problem in my localhost, there is a bug in Views filter:
Select posts whose
Published date is after or equal to: day:30

And I put it into our to-do list, our developers will take care of it, currently please try modify the filter as:
Select posts whose
Published date is after or equal to: month:1

#362372

Hi Luoy

That doesn't really solve my problem, as that seems to be counting all jobs published from the 1st December.

I'm guessing the developers haven't got to this yet, as I'm still getting some strange results..

#362625

Since you are going to get the latest post in 7 days and 30 days, I have modified the both problem view:
as below:
Select posts whose
Published date is before or equal to: day:PAST_ONE(30)

And:
Select posts whose
Published date is before or equal to: day:PAST_ONE(7)

Please check if it is what you needed

#363974

Hi Luoy

No, what I want is the posts WITHIN 7 days and 30 days, not older than 7 days and older than 30 days.

Also I think you've left on some kind of debug thing that keeps popping up on every page?

#364275

I rechecked in your website, make below modificatioins:
1) create a view "BM New Jobs by days" to list jobs posts, filter with:

Select posts with the author set by the parent View.
Select posts whose
Published date GMT is before or equal to: day:VIEW_PARAM(days)

in section "Loop Output", output same result as your previous view,
hidden link
For debug, in wpv-loop, add the result details:

<tr>
<td>[wpv-post-link]</td>
<td>[wpv-post-date]</td>
</tr>
You can remove them

2) edit the layout, view cell:
modify the codes as below:


		<wpv-loop>
          <div class = "row"> <strong>[wpv-user field="display_name"]</strong></div>
          <div class = "row"><p>Last 7 days New Jobs</p> </div>
          <div class = "row"> [wpv-view name="bm-new-jobs-by-days" days=7]</div>
          <div class = "row">  <p>Last 30 days New Jobs</p></div>
          <div class = "row">[wpv-view name="bm-new-jobs-by-days" days=30]</div>
            
		</wpv-loop>

Please test again, check if it is fixed or not.

#366112
new jobs output2.png
new jobs output.png

Hi Luoy

This doesn't seem to be working. See the screenshots attached.

It is showing posts with dates from December, which should be outside of the 30 day limit.

#366436

I checked again in your website, see the result page you mentioned in the private message box:
hidden link
It works as expected, but seems it isn't the problem page which you mentioned in your screenshot, please point out the problem page URL that you mentioned in your screenshot, thanks

#366842

Hi Luoy

I am looking at this page:

hidden link

But on both of them (including here: hidden link) I see dates on the 21st December which is not in the last 30 days, so it is not working as expected. I want to only count the posts from within the last 30 days. 21 December is not within the last 30 days.

#367078

Thanks for the details, I rechecked it in your website, you are right, it does not work as expected.
Views is using WordPress class WP_Query to query the post, see the document:
http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
The days parameter of Views is correspond to "The day of the month", but in your case, it needs a strtotime()-compatible string, for example:
after 30 days ago

So it needs custom codes with Views filter hook to apply such a filter, I added below codes into your theme/functions.php:

// https://toolset.com/forums/topic/something-strange-happening-with-a-count-of-posts/
add_filter( 'wpv_filter_query', 'my_date_filter_func', 99, 3 );

function my_date_filter_func( $query_args, $settings, $views_id ) {
    if ( $views_id == 1555 ) {
        if(isset($query_args['date_query'][0]['day'])){
			$query_args['date_query'][0] = array(
				'after' => $query_args['date_query'][0]['day'] . ' days ago',
				'column' => 'post_date_gmt',
			);
		}
    }
    return $query_args;
}

Please test again, check if it is what you need:
hidden link

#367108

Hi Luoy

I don't really understand the code, but as far as I can tell it does seem to be working, so I'll leave it at that.

Thanks for your help. I'll let you know if I notice any other problems.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.