[Resolved] Restrict shown posts to >= today

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Restrict shown posts to >= today

Tagged: 

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #31201
    J
    Participant

    Hi there,

    Is it possible to restrict the list of posts generated by wpsp by checking an ACF-field for all dates larger/equal than today?

    Based on posts on this topic I came up with this:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        $today = date('Ymd');
        if ( 123 === $settings['list_id'] ) {
            $args['meta_query'] => array(
                array(
                    'key' => 'acf_date_field',
                    'value' => $today,
                    'compare' => '>='
                )
            );
        }
        return $args;
    } );

    It’s not working. Where did I go wrong?

    J

    #31220
    elvin
    Moderator

    Hi there,

    Make sure the acf_date_field and the $today variable have the same format. Also add the type.

    Example:

        array(
        'key' => 'event_date',
        'value' => date('Y-m-d'),
        'type' => 'date',
        'compare' => '<='
      )
    #31578
    J
    Participant

    Sorry, to be so late. Tried your solution. Unfortunately it failed.

    I keep getting syntax error, unexpected '=>' (T_DOUBLE_ARROW)

    Double checked the code. Tried to split it up, no luck.

    Running: PHP 7.4.1. WPSP: 1.2.0-alpha.3

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if( 123 === (int)$settings['list_id'] ){
            $user_id = get_current_user_id();
            $args['author'] = $user_id;
            $args['meta_query'] => array(
                array(
                    'key'       => 'acf_field_name',
                    'value'     => date('Ymd'),
                    'type'      => 'date',
                    'compare'   => '<='
                )
            );
        }
        return $args;
    }, 15, 2 );

    My editor highlights the line $args['meta_query'] => array( with the unexpected error.

    Any suggestions? Or alternatives?

    Thank you for your help.

    J

    • This reply was modified 3 weeks, 3 days ago by J.
    #31601
    J
    Participant

    Hi there,

    I found a solution for my question above. In retrospect, I tried to stretch the functionality of WP Show Posts a bit too much. Sorry for that.

    Your earlier answer pointed in the right direction. Instead of adding a filter to WPSP I was able to write a function – inspired by link – able to do the following:

    • Get a list of posts
    • Created by an author
    • Filtered with a value of a custom post field
    • Sorted in the right order

    Thanks for your help!

    J

    #31613
    elvin
    Moderator

    Nice one! Glad you got it sorted. 😀

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.