Retrieving custom fields with $wpdb->get_results

Note, before going further: Take care about portability and security: function wpse50056_get_post_meta() { global $wpdb; $results = $wpdb->get_results( ” SELECT ID, post_date, post_title, post_content, meta_key, meta_value FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} ON ( $wpdb->posts.ID = {$wpdb->postmeta}.post_id ) ” ); foreach( $results as $result ) { printf( ‘<pre>%s</pre>’, htmlspecialchars( var_export( $result, true ) ) ); } … Read more

Inserting Post Meta From SQL

Actually, the customary way to do this in WordPress would be to use <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?> as per the codex: https://codex.wordpress.org/Function_Reference/update_post_meta. It yields shorter code, and the intent of the code is clearer.

Saving custom form data into database

OK, so here is how you should to this proper way… In your template file you put your form: <form id=”myForm” name=”myform” action=”<?php echo esc_attr( admin_url(‘admin-post.php’) ); ?>” method=”POST”> <input type=”hidden” name=”action” value=”save_my_custom_form” /> <select id=”brandSel” size=”1″> <option selected=”selected” value=””>– Select Brand –</option> <option>Abba</option> <option>AG Hair</option> </select> <input type=”submit” value=”submit” /> </form> And in functions.php … Read more

$wpdb->insert is changing a value

Great advice Rarst! The output of $wpdb->queries was exactly what it should be: “INSERT INTO `wp_featured_releases` (`fr_band`,`fr_album`,`fr_album_description`,`fr_image_id`,`fr_shop_url`,`fr_modified`) VALUES (”,”,”,’504′,”,’2011-01-14 10:49:58pm’)” 504 is the correct id for the image, but the value in the table is still 127. My latest thought is that my mysql skills have failed me. Images with an ID below 127 work … Read more

WP_Query to work with custom view

Use a custom view in the front-end: You can try to modify the SELECT queries in the front-end with the following (alpha) plugin: <?php /** * Plugin Name: wpdb – a custom SELECT view for the wp_posts table on the front-end * Version: alpha */ ! is_admin() && add_filter( ‘query’, function( $query ) { global … Read more

$wpdb-prepare : Do I have to bind a parameter to the table name?

No, you do not want to swap out the tablename. If you do, the table name will be wrapped in quotes and it will trigger a SQL error. Try: $table = $wpdb->prefix . ‘members’; $qry = $wpdb->prepare(“SELECT * FROM %s”, $table); var_dump($qry); $qry = “SELECT * FROM $table”; var_dump($result); The first string is invalid SQL. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)