How can I save custom meta to one global value?

Consider that when you add a metabox, nothing force you to save the value in post meta field, you can save a global option from metabox as well. function my_add_custom_box() { $screens = array( ‘post’ ); // add or replace with your cpt name foreach ( $screens as $screen ) { add_meta_box( ‘my_sectionid’, __( ‘Featured … Read more

Query Posts by Custom Field

You can achive this using WP Query with meta_query argument as following. $args = array ( ‘post_type’ => array( ‘post’ ), // YOUR POST TYPE ‘meta_query’ => array( array( ‘key’ => ‘country’, ‘value’ => $your_country, // THE COUNTRY TO SEARCH ‘compare’ => ‘LIKE’, // TO SEARCH THIS COUNTRY IN YOUR COMMA SEPERATED STRING ‘type’ => … Read more

WordPress function saves a post twice and updates all posts

It duplicate because when you run the wp_update_post(), it will use the wp_insert_post() function and the action save_post will run again. please use the filter wp_insert_post_data to filter the value before save. https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data Example: function wpse309780_filter_post_data($data , $postarr) { $data[‘post_name’] = wp_count_posts( ‘post’ )->publish; return $data; } add_filter( ‘wp_insert_post_data’, ‘wpse309780_filter_post_data’ );

meta_compare not comparing whole integer

You could try this instead: $bc_args = array( ‘post_type’ => ‘data’, ‘data-category’ =>’insurance-rate’, ‘order’ => ‘DESC’, ‘orderby’ => ‘meta_value_num’, ‘meta_query’ => array( array( ‘key’ => ‘interest_rate’, ‘value’ => (int) $i_rate, ‘type’ => ‘numeric’, ‘compare’ => ‘>=’ ), ) ); $sortedPosts = new WP_Query( $bc_args ); where we use the numeric type. You should also consider … Read more

Hooking into the post editing screen for an existing page only

You should really only need to run this on an existing post, since new posts will be created in the manner you specify. add_action( ‘load-post.php’, ‘custom_content_conversion’ ); function custom_content_conversion() { if (! empty($_GET[‘post’]) ) { // Get the post object $post = get_post($_GET[‘post’]); // If the post object has content, store it in the meta … Read more