Articles of sql

Database “Migration” for Plugins?

I’m creating a plugin for WordPress. This plugin will need to save data to the database. In other programming frameworks I’ve worked with, there are systems called “database migrations”. These migrations are SQL ALTER TABLE or CREATE TABLE statements/scripts Sometimes abstracted into the programming language of the framework Written and stored in a common way […]

Overriding default calendar to show posts from a category

I’m trying to modify the default WP calendar (get_calendar()) so that it shows posts from a specific category and then use that as a shortcode. Here’s what I’ve done: Copied across the get_calendar() function from the core (from wp-includes) into my child theme’s functions.php file. In order to make it an ‘independent’ calendar, I changed […]

WPML tables not deleted when removing multisite subsite, need help clearing up my sql-file!

I´m building a WP Multisite for a client that wants a multilingual site. Before choosing WP Multisite, I bought and tried out WPML. However that plugin didn’t fit for the job so I deleted it and got a refund. However, now this seems to come back pestering me somehow. Now when using Multisite, I have […]

$wpdb get_var issue

I’ve been at this for while, but can’t figure out why the below isn’t working. I have my date saved as a ‘MM/DD/YYYY’ string. global $wpdb; $new_day = $day->format(“m/d/Y”); $qty = $wpdb->get_var( $wpdb->prepare( ” SELECT qty FROM $wpdb->specials WHERE date = %s “, $new_day ));

Export entries and multiple custom field meta to .csv?

First time poster. I’m trying to export a bunch of custom posts out to a csv file so I can easily import them into a new database. I’ve ran the native export to xml, but it’s not the correct format I need. I need a column based format (ie. name, height, weight, etc.). I have […]

Need help writing a $wpdb query

I am trying to search through post meta for keyword matches but I’m new to MySQL and I don’t know how to write my query. I can write a basic query for post_title matches but I don’t know how to properly jump across tables. In pseudo code, my query would be “SELECT post_id FROM $wpdb->postmeta […]

Query that joins the postmeta table twice

I believe what I’m trying to do is too complex for the standard meta_query parameter in get_posts, so I’m trying to use a custom SQL query to do the following: I have a game custom post that has custom fields for team1 and team2 stored as postmeta. Given team XXXX, I need to pull all […]

Use wpdb->prepare for `order by` column name

In my current plugin, I am picking up the order by column as user input. So I thought of using wpdb->prepare to escape it. $wpdb->get_results($wpdb->prepare(“Select id from $wpdb->posts order by %s %s”, $order_by_col, $order_by); This is not working because it gets turned into select id from wp_posts order by ‘post_date’ ‘asc’ (Note the quotes) So […]

How to generate page content from database the right way?

I am creating many pages and store them into the database. I am first styling them and than storing them. The idea is to use page.php to check the page id and display the needed post. This is all good, but I have many pages in the website. WHich means I have to perform the […]

How to use $wpdb to delete in a custom table

I am having trouble finding a good resource on using the $wpdb function. I am trying to delete a row from a custom table named: eLearning_progress $removefromdb = $wpdb->query(“DELETE FROM eLearning_progress WHERE ID = ‘$user_id’ AND module_id = ‘$singlecomparearrays_remove'” ); The row I would like to delete has the ID of ‘$user_id’ and the ‘module_id’ […]