Is it possible to update the dataset using update_post_meta

You don’t need your second foreach loop. foreach( $_POST[‘wt_set’] as $name => $val ){ update_post_meta( $post_id, $name, $val ); } What’s happening is with your second foreach is each entry is being processed individually and set as the post meta, so as it’s looping through it’s running update_post_meta( $post_id, $name, XXX ); then update_post_meta( $post_id, … Read more

is_front_page is not working in my functions.php

add_action(‘init’, ‘save_landing_page_slider’, 10, 2); init is too early — if you look at the WordPress query overview on Codex, init is (as of writing) the second step, whereas is_ variables like $is_page (or WP_Query::$is_page) that are used by conditional tags like is_front_page() are only set in step 4, so instead of init, you should use … Read more

How to get latest post ID in functions.php?

Here is the code that should probably work for you: function bt_update_user_homepage_meta() { // Get user id $user_id = get_current_user_id(); if ( !empty( $user_id ) && is_page( [ ‘home’ ] ) ) { $posts = wp_get_recent_posts( array( ‘numberposts’ => 1, ‘meta_key’ => ‘usp-custom-1’, ‘meta_value’ => ‘question’ ) ); if ( !empty( $posts ) ) { … Read more

Multiple AJAX handler functions conflict in functions.php

The use of $_POST[‘action’] in the WP_Query and as a dynamic action name is dangerous, and in the code triggers the handler on all AJAX requests even those you don’t want. This is unavoidable and a dead end that cannot be fixed. Alternatives are mandatory. Instead, use a static non-dynamic action name, E.g. mathieu_loadmode_post_archive or … Read more

localize_script but data changes dependent on product ID

You should call wp_localize_script() in the wp_enqueue_scripts hook (or a later hook) in which you would get the correct product/post data such as ID, and that conditional tags like is_single() would also work as expected. So you should move all the $product, $product_id, $dataToBePassed and wp_localize_script() parts to inside the wp_enqueue_scripts callback, and then you’d … Read more