questions

If custom field value already exists today, then prevent from posting (WP)

Rate this post

Basically I’m trying to prevent creating duplicate posts.

So, if today is March 13, 2021 and someone just created a post with a custom value “Apple”.

If you try again creating a post that same date with the same custom field value, it won’t let you do it.

Basically show an error message in the editor.

I need this code to be added into functions.php.

Here’s what I’ve got so far:

I know how to recognize if a custom field exists:

if ( get_post_meta( $post->ID, 'fruitoptions', true ) )

I know how to retrieve today’s date:

$args = array(
    'year' => (int) date('Y'),    
    'monthnum' => (int) date('m'),  
    'day' => (int) date('d'), 

What I can’t find is so how to prevent WordPress editor to publish the post if the above data is detected.

And basically combine everything into one code.

Any help would be appreciated.

 

✔️Solution:

$meta_values = get_post_meta( $post->ID, 'fruitoptions', true );
var_dump( $meta_values );
foreach($meta_values as $key=>$val){
...
}

I’m not so familiar with WordPress. Just check the var_dump. Maybe you will get the Date and e.g. Apple.

Leave a Reply

Your email address will not be published.

Back to top button