We're merging with GenerateBlocks! Learn more here.

[Support request] Retrieve WPshowposts by meta key value (a date) compared to DateTime(now)

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Retrieve WPshowposts by meta key value (a date) compared to DateTime(now)

  • This topic has 1 reply, 2 voices, and was last updated 3 years ago by Tom.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #19895
    Ian
    Participant

    I have custom post metadata with the name acnb_live_date. I want to use WP Show Posts to retrieve a post list based on a comparison between acnb-live_date and (now)+ nS. The code I need to execute would be something like this. I lack sufficient understanding of the Loop to work out where I need to run it. Ideally I’d like it only to run when the show posts function runs, but if I could handle it being a site wide thing.

    $acnb_live_date = get_post_meta( get_the_ID (), ‘acnb_live_date’, true);

    if (isset($acnb_live_date)){

    $compare_date = new \DateTime(); //now
    $compare_date->add(new \DateInterval(‘PT3600S’));//add 3600s / 1

    $cc_args = array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘post’,
    ‘meta_key’ => ‘acnb_live_date’,
    ‘value’ => date(“Y-m-d”)
    ‘compare’ => ‘>= compare_date’
    ‘type’ => ‘date’
    );

    $cc_query = new WP_Query( $cc_args );
    }

    THank you so much for your continuing development of these project. They are powerful and helpful.

    #20033
    Tom
    Keymaster

    Hi there,

    You may need to filter the query itself.

    First, install the latest testing version here: https://wpshowposts.com/wp-show-posts-1-2-0/

    Then you can do this:

    add_filter( 'wpsp_query_args', function( $args, $settings ) {
        if ( 123 === (int) $settings['list_id'] ) {
            $args['meta_key'] = 'acnb_live_date';
            $args['value'] = date( 'Y-m-d' ),
            $args['compare'] = 'compare_date';
            $args['type'] = 'date';
        }
    
        return $args;
    }, 10, 2 );

    You just need to change the 123 to the ID of your list, then you have full control over the query.

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