How to prevent multiple post with same meta value being created simultaneously in WordPress (with ajax)

first of all at the end of using a new WP_Query it’s good pratice to always use wp_reset_postdata(); to be sure nothing had compromise your database. $already_reserved = new WP_Query(array( ‘post_type’ => ‘appointments’, ‘fields’ => ‘ids’, ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘datetime’, ‘value’ => $date_time, // timestamps ‘type’ => ‘numeric’, ‘compare’ … Read more

Call product search from custom hook

What I understood is that no AJAX is needed. If somebody don’t understand all the fancy example around, you just need to use the pre_get_posts hook. In my case what I did is: function custom_product_search($query) { $query->query_vars[‘post_parent’] = htmlentities($_GET[‘model’]) ?? htmlentities($_GET[‘make’]); return $query; } add_action(“pre_get_posts”, ‘custom_product_search’); I don’t know if this is the correct solution … Read more

Why Ajax Doesn’t Work?

You should move this code into a new plugin instead, e.g. save this file as wp-content/plugins/example-ajax-handler.php <?php /* Plugin Name: Example AJAX handler Description: Handle example_ajax_request admin-ajax requests Version: 0.0.1 */ add_action( ‘wp_ajax_example_ajax_request’, ‘example_ajax_request’ ); add_action( ‘wp_ajax_nopriv_example_ajax_request’, ‘example_ajax_request’ ); function example_ajax_request() { if ( isset( $_REQUEST ) ) { $postID = $_REQUEST[‘postID’]; print $postID; } … Read more

Populate Product Regular Price with a calculated ACF Field Value

Based on a reference, I was able to populate the field of the regular price based on ACF field alue using the following Code : add_action( ‘woocommerce_process_product_meta’,update_product_price_from_acf’, 100, 1 ); function update_product_price_from_acf( $product_id ) { if( $price_acf = get_field( ‘netsalesprice’, $product_id ) ) update_post_meta( $product_id, ‘_regular_price’, $price_acf ); } This, however, needs me to go … Read more