MySQL queries in WordPress

I found the errors thanks to the help from @Milo @Milo’s query on catno pointed me to my error with my first query involving catno, and I also noticed a double equals in $catno == $row[0]; when it should be $catno = $row[0]; Final coding is function QueryStock($cat) { $query = “SELECT * FROM stock … Read more

Converting Posts to Pages

A while ago I added a function to one of my sites to change a set of posts from one type to another. I can’t remember exactly why, but I’ve dug it out and updated it a little, and it is tested and working. The function Place this in functions.php so that it can be … Read more

Custom redirects with using SQL

I wrote function for short url. Maybe it will be helpful for someone: function short_url(){ global $post; if (preg_match(‘/^http:\/\/example.com\/page\/[0-9]{1,4}$/’, “http://”.$_SERVER[“HTTP_HOST”] . $_SERVER[“REQUEST_URI”])) { $my_id = intval(preg_replace(‘/[^0-9]+/’, ”, $_SERVER[“REQUEST_URI”]), 10); global $wpdb; $myquery = $wpdb->get_row($wpdb->prepare(“SELECT * FROM wp_mytable WHERE ID=%d”, $my_id)); $short_url=”http://example.com/?page_id=”.$myquery->page_id.”&user_id=”.$myqery->user_id; wp_redirect($short_url); exit(); } } add_action( ‘template_redirect’, ‘short_url’ );

WordPress upload images not displaying

For a quick solution you can download the WordPress plugin photo-gallery https://wordpress.org/plugins/photo-gallery/. Once it’s installed and activated you hover your mouse over Photo Gallery in your WordPress Dashboard and click Add Galleries/images. Add a new picture or many pictures in a new gallery and save. When it’s done simply hover your mouse over Photo Gallery … Read more

Function returning queried meta value based on current post ID

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