Support » Plugin: Posts in Sidebar » Query posts with date as custom field

  • Resolved pavelevap

    (@pavelevap)


    Hi,

    I am impressed about possibilities of this great plugin.

    Is it possible to query posts with date as custom field and condition to show only posts with future date from now (example upcoming events)? Maybe I missed it somewhere in settings, I am not sure…

    Thank you!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Aldo

    (@aldolat)

    Hi,
    in theory it’s possible. Which is the format of the date stored in the custom field?

    2018-05-25 00:00

    I tried several options, but I did not achieve any result.

    Plugin Author Aldo

    (@aldolat)

    You can use the panel “Getting posts” > “Custom fields query”, for example see this screenshot:

    screenshot

    As you can see, I have:
    – custom field key A1 = date
    – custom field value = 2018-05-25 00:00
    – operator = > (greater than)
    – type = DATETIME

    This query returns posts that have a custom field key date where its value is greater than 2018-05-25 00:00.

    Try and let me know.

    Yes, I played with this section, but I need to query posts with custom field greater than current date, something like NOW().

    Plugin Author Aldo

    (@aldolat)

    Unfortunately that’s not possible. If the posts you want to get are unpublished, you could query posts with “Scheduled” status and with custom field date (or any other custom field).

    I looked at your code and maybe filter for $meta_query would be helpful here?

    https://github.com/aldolat/posts-in-sidebar/blob/d37210cfd7f0428b98a713eb041ce4b37476f9d7/includes/pis-functions-queries.php#L365

    I could hook into $meta_query and change $args['mq_value_aa'] on the fly?

    Another issue will be probably ordering. When DATETIME is used as meta_type, then I should be able to set orderby to meta_value_datetime somehow?

    Plugin Author Aldo

    (@aldolat)

    Great idea, @pavelevap! I should add more filters like this.

    I added a filter before returning $meta_query.

    I just pushed this commit to GitHub and now you can use a function like this:

    
    function pis_edit_meta_query( $args ) {
    	$args[0]['value'] = date( 'Y-m-d H:i' );
    
    	return $args;
    }
    add_filter( 'pis_meta_query', 'pis_edit_meta_query' );
    

    This function will change the value with the current date and time, regardless of what has been entered in the widget.

    The commit is in the develop branch, so you should manually download the update or simply change the line in your plugin installation.

    If you confirm me that all works as intended, I will add this in the master branch.

    About the ordering, I just used:
    – “Getting posts” > “Order posts by” > “Meta value”
    – “Getting posts” > “The order will be” > “Descending” (or “Ascending”)
    and the posts are ordered by this meta value.

    Please, let me know.

    • This reply was modified 9 months, 2 weeks ago by Aldo.

    Thank you very much, it works nice!

    But I would need one more filter to be able to play with output. I need to make DATETIME format a little bit friendlier for visitors, for example 4th March 2019 (13:00). I tried get_post_metadata filter, but I am not able to define context only for widget of your plugin and not all usages of this postmeta key 🙁

    I am not sure about the best place for this filter, maybe $the_custom_field

    https://github.com/aldolat/posts-in-sidebar/blob/d37210cfd7f0428b98a713eb041ce4b37476f9d7/includes/pis-functions-display.php#L301

    or final $output?

    https://github.com/aldolat/posts-in-sidebar/blob/d37210cfd7f0428b98a713eb041ce4b37476f9d7/includes/pis-functions-display.php#L329

    Plugin Author Aldo

    (@aldolat)

    I’ve added a new filter exactly here and it’s named pis_custom_field_value.

    The filter lets you change the date and the time before they are displayed.

    In the functions.php file of my test theme, I tested this function:

    function pis_make_datetime_human_readable( $datetime ) {
    	// Get the date and the time formats as they are defined in the WordPress dashboard.
    	$date_format = get_option( 'date_format' ) . ' (' . get_option( 'time_format' ) . ')';
    	// Convert $datetime to UNIX timestamp, as requested by date_i18n() later.
    	$datetime = strtotime( esc_html( $datetime ) );
    	// Convert UNIX timestamp according to my date and time formats.
    	$datetime = date_i18n( $date_format, $datetime );
    
    	return $datetime;
    }
    add_filter( 'pis_custom_field_value', 'pis_make_datetime_human_readable' );

    It echoes this output:

    4th March 2021 (13:50)

    as you requested. 🙂

    Let me know, please.

    Awesome, it just works! Now it is flexible for all kinds of events, etc. Thank you very much!

    Plugin Author Aldo

    (@aldolat)

    @pavelevap you’re very welcome!

    Thank you for your posts! 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Query posts with date as custom field’ is closed to new replies.