[Support request] how to set up a custom parameter?

Home Forums Support [Support request] how to set up a custom parameter?

Home Forums Support how to set up a custom parameter?

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #2302689
    Chris

    New with Generate Press (migrating from wp show posts) and I need some help understanding how to implement a custom parameter.

    I create my posts using Formidable Form’s front end editor.

    I have a custom field (post meta-data) so that I can include an expiration date on some of my posts that are timely and I don’t want them to appear in the generated block after a certain date.

    I need to add a parameter that excludes posts if the expiration date has passed. So… if expiration date is less than current date, exclude post.

    Under the ‘add parameter’, I see a lot of standard options, but not a way to add a custom parameter based on a custom field like this. I looked through the documentation and don’t see anything along these lines (apologies if I missed it). This seems like something I should be able to do with generate blocks… would I need to do this through a snippet and can you provide an example if so?

    thanks,

    Chris

    #2302691
    Fernando
    Customer Support

    Hi Chris,

    Are you referring to the Query Loop block?

    If so, you may use generateblocks_query_loop_args filter to add a new parameter.

    For instance:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    		if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-query' ) !== false ) {
    			$query_args['date_query'] = array( 'after' => '2012-03-01' );
    		}
    
    		return $query_args;
    	}, 10, 2 );

    You’ll need to add my-query to the class list of the Grid Block within the Query loop block.

    You can try grabbing the post meta for your expiry date and modify the code to your preference.

    #2303862
    Chris

    Thanks Fernando,

    You said:

    > You’ll need to add my-query to the class list of the Grid Block within the Query loop block.

    Can you provide more details or documentation on that? I see a setting in the query loop block to add a css class, but I don’t think that’s what you mean, or do you?

    Chris

    #2303873
    Fernando
    Customer Support

    It’s in the advanced section of the Grid Block’s settings.

    Here’s an example: https://share.getcloudapp.com/7Ku65l4m

    #2305421
    Chris

    Fernando,

    Sorry, I’m still not getting it.

    What I have is below. I commented out some of it just for testing, to make sure the values are there. As it is, no articles should appear since the expiration date has passed, but they all do.

    Eventually of course I want it to only eliminate the articles that are expired (mostly holiday related posts that I don’t want to show on the home page once the holiday has passed).

    Also, I question, with this being a css class, is it only going to hide these posts but leave them in the html source? I really would prefer they be completely removed, for seo purposes.

    Thanks for help and clarification!

    edit – I do have my-query set as an additional CSS class for the grid
    ———————————–

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    	
    //	global $post;		
    //	$post_id = $post->ID;
    //	$expire_date = get_post_field('expire_date', $post_id);
    	
    	$expire_date = '06/20/2022';
    	$curr_date = date("m/d/Y"); 
    	
    	if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-query' ) !== false ) {
    			$query_args[$curr_date] = array( 'after' => $expire_date );
    		}
    
    		return $query_args;
    	}, 10, 2 );
    #2305663
    David
    Staff
    Customer Support

    Hi there,

    WP_Query has the 'meta_query' $arg, for example:

    https://wordpress.stackexchange.com/a/278557

    Which your should be able to include like so:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    	if ( 
                ! is_admin() &&
                // if block has an advanced > additional CSS Class of: my-query
                ! empty( $attributes['className'] ) && 
                strpos( $attributes['className'], 'my-query' ) !== false
    	) {
                // pass meta_query parameter
                $query_args[ 'meta_query' ] = array(
                    'meta_key'          => 'expire_date',
                    'type'              => 'DATETIME',  // You can also try changing it to TIME or DATE if it doesn't work
                    'meta_value'        => date("m/d/Y"),
                    'meta_compare'      => '>=',
                );
    	}
    	
    	return $query_args;
    }, 10, 2 );

    In our IF condition where checking whether the block has a Class attribute of my-query ( you can change that to whatever you need ).
    IF that condition is met, then we pass our 'meta_query' arg into the query loop parameters.

    #2306043
    Chris

    Thanks David,

    I usually pick this stuff up pretty good… but..

    If I use your code exactly as above, and print_r($query_args), I get:

    Array ( [post_type] => post [posts_per_page] => 10 [paged] => 1 [offset] => 0 [meta_query] => Array ( [meta_key] => expire_date [type] => DATE [meta_value] => 08/07/2022 [meta_compare] => >= ) )

    Is that as expected? Does the [meta_key] => expire_date get that value from each post in the loop that wp_query does?

    If so, this doesn’t seem to be working. I’ll provide further details in the private info section.

    #2306547
    David
    Staff
    Customer Support

    Is that as expected? Does the [meta_key] => expire_date get that value from each post in the loop that wp_query does?

    That looks correct, and yes the [meta_key] => expire_date will return the post meta value of the expiry_date – do you know if that meta_key name is correct ?

    #2306665
    Chris

    Yep, I double checked that

    #2306680
    David
    Staff
    Customer Support

    Do you know what format it returns ? As i believe for date comparisons those have to match ?

    #2308380
    Chris

    I checked what format it returns using:

    global $post;
    $post_id = $post->ID;
    $expire_date = get_post_field(‘expire_date’, $post_id);

    and returning the $expire_date in a function on the post with an expire_date – I found it was returning it as: 2022-06-20

    So I changed the date format to: ‘meta_value’ => date(“Y-m-d”),

    However, it’s still not working…. the post is not being excluded.

    Just as a test, I changed the query args to Fernando’s example: $query_args[‘date_query’] = array( ‘after’ => ‘2022-06-01’ );

    and that works – it excludes any posts published before 2022-06-01 – but of course, that’s not what I need it to do. But that shows that the class and function is working. It just seems like the query isn’t doing what is expected.

    I did try DATETIME/DATE/TIME as well,

    Chris

    #2308510
    Fernando
    Customer Support

    I see. How about using a plugin for the expiration date of a post? Example: https://wordpress.org/plugins/post-expirator/

    Such plugins allow you to add an expiration date for posts to unpublish them.

    In this case, unpublished posts will automatically be excluded in your loop.

    #2308857
    Chris

    I don’t want to unpublish it though… just exclude it from the loop.

    I’ll keep experimenting, but I am willing to pay for someone to dig into this and help on site if anyone is interested. I can be contacted at chris@andrews.com

    Chris

    #2310504
    Chris

    Okay, got it working now –

    According to an answer I received over at stackexchange:

    —–
    Note that meta_query expects nested arrays, even if you only have one query.

    So your meta query should actually look like the following, and note that in a meta_query, we don’t use the meta_ prefix, e.g. we use just key and not meta_key:
    —-

    $query_args['meta_query'] = array(
        array( // clause/query 1
            'key'     => 'expire_date',
            'value'   => date("Y-m-d"),
            'type'    => 'DATE',
            'compare' => '>=',
        )
    );

    I don’t quite understand why it has to be these way and the other way didn’t work, but thought I would pass this along in case anyone else finds it helpful,

    Chris

    #2310727
    David
    Staff
    Customer Support

    Aah i see, that makes sense as the meta_query defines it’s a meta value comparison, so theres no need for the prefixes.
    Glad to hear thats working, and thanks for sharing the solution!

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