Save customizer default values to DB on theme activation
Save customizer default values to DB on theme activation
Save customizer default values to DB on theme activation
I will not worry to much about performance here, but I will optimize the code a bit by calling get_post_meta() only once. This will not increase or decrease performance as you will see in the post I have linked to later on, but it comes down to the principle of not repeating yourself. On the … Read more
add_action( ‘admin_footer’, ‘my_action_javascript’ ); // Write our JS below here function my_action_javascript() global $wpdb; $pageposts = $wpdb->get_var(“SELECT max(menu_order) FROM $wpdb->posts WHERE post_status=”publish” AND post_type=”post_trainers””); ?> <script type=”text/javascript” > jQuery(document).ready(function($) { var data = { ‘action’: ‘my_action’, ‘whatever’: <?=$pageposts?> }; $.post(ajaxurl, data, function(response) { if($(‘#menu_order’).val()==” || $(‘#menu_order’).val()==’0′) $(‘#menu_order’).val(response); }); }); </script> <?php } add_action( ‘wp_ajax_my_action’, ‘my_action_callback’ … Read more
You can still do the AS with $wpdb, I find it still makes stuff look cleaner. You can use the $wpdb->prepare() method for sanitation. Are you looking for something like this? function combined_downloads($post_id) { global $wpdb; return $wpdb->get_var($wpdb->prepare( “SELECT SUM( meta_value ) FROM $wpdb->postmeta AS m LEFT JOIN $wpdb->posts AS p ON m.post_id = p.ID … Read more
Assuming that all of your sql column names are correct, something like this should work: if(isset($_POST[‘submit’])){ global $wpdb; $tablename= $wpdb . ‘form_subscribe’; $myrows = $wpdb->get_var( $wpdb->prepare(“SELECT email FROM $tablename WHERE email=%s LIMIT 1″, $_POST[’email’])); if(empty($myrows)){ $data=array( ‘name’ => $_POST[‘fullname’], ‘age’ => $_POST[‘age’], ’email’ => $_POST[’email’] ); $wpdb->insert( $tablename, $data); } else { $status=”User already subscribed”; … Read more
Inserting and updating rows with wpdb indreases integer fields by 1 point sometimes
My solution to the problem: $prepare = array(); $in = implode(‘,’, array_fill(0, count($product_ids), ‘%d’)); foreach ($product_ids as $ids){ $prepare[] = $ids; } $prepare[] = “post”; $prepare[] = $num; $results = $this->db->get_results($this->db->prepare(“SELECT ID, post_title FROM {$this->db->posts} WHERE ID NOT IN({$in}) AND post_type=%s ORDER BY ID DESC LIMIT %d”, $prepare));
Run insert if no entry otherwise run update with ajax
short exmaple how i do it… (tables naming not really correct.) in this example i am trying to save _description $_POST variable. add_action ( ‘edited_term’, ‘custom_edited_term’, 10, 3); function custom_edited_term($term_id, $tt_id, $taxonomy){ if ( defined(‘DOING_AJAX’) || defined(‘DOING_CRON’) ) return; $_POST = stripslashes_deep($_POST); if (isset($_POST[‘_description’]) && trim($_POST[‘_description’]) != ”){ update_term_meta($term_id, ‘_description’, trim($_POST[‘_description’])); } else { delete_term_meta($term_id, … Read more
Object Cache – Avoid db queries totally