WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 8 months ago

#29319 new enhancement

filter dayswithposts in widget calendar

Reported by: kkarpieszuk Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.0
Component: Widgets Keywords: needs-unit-tests has-patch needs-refresh
Focuses: Cc:

Description

Hello

here is Konrad, WPML developer. We are fighting now with bug related WordPress calendar widget: it always displays all posts, and we want to filter its results regarding to language of post.

Steps to reproduce:

  1. install WPML
  2. publish some post in En language July 1st
  3. translate this post July 2nd to Polish language
  4. activate calendar widget

Result: calendar will show that there are posts in July 1st and 2nd. Expected result: when user displays English posts, calendar should display only July 1st. When user switches to Polish language, calndar should display only July 2nd.

We want to fix this in WPML but we need filter inside of get_calendar() function.

Wordpress gets days with posts by this query inside of get_calendar():

$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
        FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
        AND post_type = 'post' AND post_status = 'publish'
        AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);

The result should be able to filter, then WPML will hook here.

I am attaching proposed diff file.

(I see that we can hook into get_calendar filter but this variable has full html output of calendar. This will unefficient to parse this HTML)

Attachments (3)

dayswithposts_filter.diff (568 bytes) - added by kkarpieszuk 4 years ago.
proposed diff file
general-template.diff (3.6 KB) - added by podpirate 4 years ago.
Filter where and join clause in get_calendar()
get_calendar-get_posts.diff (6.6 KB) - added by podpirate 3 years ago.
use get_posts() in get_calendar()

Download all attachments as: .zip

Change History (26)

@kkarpieszuk
4 years ago

proposed diff file

#1 @SergeyBiryukov
4 years ago

  • Type changed from feature request to enhancement

The filter needs to be documented as per the documentation standards.

As a workaround, you can use the 'query' filter to change that query.

#2 @podpirate
4 years ago

Hi Konrad,

I am struggeling with a similar issue in one of my plugins (WP Access Areas). I would propose to filter the WHERE and JOIN clause similar to getarchives_where in function wp_get_archives(). I'll submit a patch instantly.

@podpirate
4 years ago

Filter where and join clause in get_calendar()

#3 @kkarpieszuk
4 years ago

For me this, what @podpirate proposed looks good and solving also my problem

#4 @SergeyBiryukov
4 years ago

  • Milestone changed from Awaiting Review to 4.1

#5 @wonderboymusic
4 years ago

  • Keywords needs-patch added

I was about to commit this, then realized: this can use date_query if we offer a more robust way to select fields as aliases.

If we keep it in WP_Query, the args can be filtered, instead of having to filter SQL fragments.

#6 @johnbillion
4 years ago

  • Milestone changed from 4.1 to Awaiting Review
  • Version changed from 4.0 to 3.0

I'm not a fan of introducing a bunch of SQL query filters for the calendar. I'd rather look into switching this over to a date query as wonderboymusic suggested.

@podpirate
3 years ago

use get_posts() in get_calendar()

#7 @podpirate
3 years ago

I just submitted a patch where get_calendar makes use of get_posts rather than custom built SQL. Works fine with my plugin out of the box (@kkarpieszuk and hopefully with WPML as well).

#8 @chriscct7
2 years ago

  • Keywords needs-unit-tests added

#9 @SergeyBiryukov
15 months ago

#15202 was marked as a duplicate.

#10 @SergeyBiryukov
15 months ago

#40059 was marked as a duplicate.

#11 @BackuPs
15 months ago

regarding https://core.trac.wordpress.org/ticket/40059

Hi

Thanks for the heads up on this. But that ticket is already open for more then 3 years. Is there any progress on this?

Looking forward to your reply on that.

#12 follow-up: @kkarpieszuk
14 months ago

Hi, sorry. Notification about this ticket didn't hit my mail inbox.

I see @podpirate prepared working patch. Could this be commtied?

#13 in reply to: ↑ 12 @BackuPs
14 months ago

Replying to kkarpieszuk:

Hi, sorry. Notification about this ticket didn't hit my mail inbox.

I see @podpirate prepared working patch. Could this be commtied?

Yes the @podpirate solution works fine. I unregistered the default wordpress calendar widget and added my own calling that function with his adjustments. It works just fine and is picking up any pre_get_post action.

It is unbelievable that the wordpress core team did not fix this issue for such a long time. But i see many other issues reported and acknowledged which also do not get fixed for even a longer period of time. I wonder what is withholding them for fixing this.

#14 @SergeyBiryukov
14 months ago

  • Keywords has-patch added; needs-patch removed
  • Milestone changed from Awaiting Review to 4.8

#15 @BackuPs
14 months ago

There is one flaw in @podpirate fix code fix. When there are no posts found the calendar does not show al all.

This code should be changed to have at least show the calendar with todays date

<?php
if ( ! $gotsome ) {
        //$cache[ $key ] = '';
        //wp_cache_set( 'theme_get_calendar', $cache, 'theme_calendar' );
        //return;
}

It is the same with the default wp calendar as it comes out of the box.

Last edited 14 months ago by BackuPs (previous) (diff)

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


14 months ago

#17 @SergeyBiryukov
14 months ago

  • Keywords needs-refresh added

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


13 months ago

#19 @jbpaul17
13 months ago

  • Milestone changed from 4.8 to 4.8.1

Punting to 4.8.1 per bug scrub earlier this week.

#20 @westonruter
12 months ago

  • Milestone changed from 4.8.1 to 4.9

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 months ago

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 months ago

#23 @westonruter
8 months ago

  • Milestone changed from 4.9 to Future Release
Note: See TracTickets for help on using tickets.