Tag Info

Hot answers tagged

4

You want to create Woocommerce products, and to do a good job you don't need only the post object and the thumbnail: you need taxonomies and custom fields, too: I think your product will have - at least - a price, isn't it? Once you want to bulk create products I suppose that having same taxonomies and same custom fields for all of them is good for you ...


4

Why don't you use WordPress get_posts? You can add the parameter orderby => 'title' as you want. $args = array( 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'title' ); $posts = get_posts( $args ); print_r($posts); If you want the sql you can just add ORDER BY post_title: SELECT * FROM $wpdb->posts ...


3

If the code creating new tables uses dbDelta() (it should), you can filter the query (see wp-admin/includes/upgrade.php): add_filter( 'dbdelta_create_queries', function( Array $queries ) { foreach ( $queries as $table => $query ) { if ( FALSE !== stripos( $query, 'CREATE TABLE' ) ) { // change the query here } ...


3

Look at your conditions: WHERE ( ( (meta_key='user_school_subjects' AND meta_value LIKE '%history%') OR (meta_key='user_professional_courses' AND meta_value LIKE '%history%') OR (meta_key='user_language_tutoring' AND meta_value LIKE '%history%') OR (meta_key='user_music_tutoring' AND meta_value LIKE ...


3

prepare() takes at least 2 parameters. Most of your calls to prepare() are not needed. For example: $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs")); In this code, if the call to $wpdb->prepare() were valid, it wouldn't do anything. The function doesn't do anything to the first parameter. It manipulates the values ...


3

You can update the post_type in mysql table wp_posts update the post_type to products sample query UPDATE wp_posts SET post_type = 'products' WHERE post_type = 'post';


3

I'd agree that doing this in SQL would be a lot more to learn than PHP. With WordPress functions you can run a query on all the images ( WP_Query ) and then loop through the results and use the info from the images to create a new post using [wp_insert_post()][2]. Finally you can update that post's _thumbnail_id meta key ( [update_post_meta()][3], which ...


3

I know it might be abit late for answer but I came across similar issue while making my test project. Here's how I solved it. /* apply this filter only on relevant to you pages */ function mb_bail_main_wp_query( $sql, WP_Query &$wpQuery ) { if ( $wpQuery->is_main_query() ) { /* prevent SELECT FOUND_ROWS() query*/ ...


2

wpdb doesn't include such kind of API, mostly since it descended from ezSQL which didn't either (as far as I remember). While there are some helpers for insert/update stuff, most of query abstraction happens in WP_Query since that's where bulk of complicated querying is typically going on.


2

If I'm not missing something, the below should work for you. It is pretty much straight from the Codex: WP_Query - Custom Field Parameters. Do it like this: $args = array( 'post_type' => 'post', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'color', 'value' => 'red', ...


2

I am going to guess that you have your code in the theme, probably functions.php. That code will only load when the theme is active, and after_switch_theme only runs after the theme change. There is no before_switch_theme that I am aware of. You will need to have this code in a plugin or a mu-plugin file in order to have it work for all themes. But ...


2

You want the query to look like this: SELECT email FROM wp_my_users WHERE email = 'mail@example.com' instead of this: SELECT email FROM 'wp_my_users' WHERE email = 'mail@example.com' So try to construct your query with: $sql = "SELECT email FROM {$my_table_name} WHERE email = %s"; $result = $wpdb->get_var( $wpdb->prepare( $sql, $email_address ...


2

Why not use get_comments(), instead of trying to roll your own with $wpdb? function display_sitewide_comments() { $sites = wp_get_sites(); $network_comments = array(); $max = 20; foreach( $sites as $site ) { switch_to_blog( $site->blog_id ); $args = array( 'number' => $max, 'status' => ...


2

MySql DELETE syntax is not very different fiom SELECT, so you can delete from multiple tables using a single query. Taxonomies informations in WordPress are in 3 tables: wp_terms wp_term_taxonomy wp_term_relationships the first 2 contain term / taxonomy informations, the 3rd contains association between terms and posts. The query to delete all tags ...


2

An update for Wordpress 3.9+ - with PHP 5.5 deprecating the mysql_* functions Wordpress has begun using the mysqli if it is available on the server. Your future plugins and code should take this into consideration and stop using mysql_*. Right now, in 3.9, wordpress checks for the availability of mysqli and uses it if available. If not it uses mysql_* but ...


2

Its not that $wpdb->query does not need $wpdb->prepare(), it's because of not using $wpdb->prepare() in a correct manner. In your case $wpdb->prepare() never received any values which it must. Please refer to the Codex for details about using $wpdb->prepare().


2

Here, you can get adjacent post for specific post type with custom sql query & with filter get_{$adjacent}_post_where where default adjacent is previous. Also result depends on $current_post_date & comparison operator $op. function bm_get_adjacent_post( $post_id, $author_id, $previous = 1 ) { global $wpdb; if ( ( ! $post = get_post( $post_id ) ) ...


2

If you are merely trying to get a count, $wpdb->get_var(); along with using COUNT() in your sql will be better: ### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); return $rowcount; } ...


2

Here are a few things I use to optimize server-side on high-traffic sites. I've worked on WP sites with over 300K posts, so you are fine as long as your server is configured properly. I would recommend installing a caching plugin like W3 Total Cache. I've had great luck with this on large enterprise sites. Install memcached on your server for object and ...


2

You can use the plugin Debug Objects and his query function to identify the slowest queries from core and plugins. I see very often the problem on a plugin or theme functionality. Identify and solve.


2

How can I write a bulk MySQL command to add in the value wp_capabilites='a:1:{s:10:"subscriber";b:1;}' into each user_id except 1, 2 and 3 ie. the newly imported users? You don't. That is a serialized array which is a PHP construct. MySQL has no idea what to do with it. To the database, it is just an string. To PHP it is a representation-- a ...


2

Since version 3.9 (that will be released soon, actually in beta 2) WordPress will use mysqli to connect to database (3.9- versions use mysql) so you can actually use new wpdb instances and helper function to connect externa values from WordPress. What I suggest is to configure external database using constants in wp-config.php in this way you are sure that ...


2

You can use Query Monitor plugin From Plugin Documention: Here's an overview of what's shown: Database Queries Shows all database queries performed on the current page Shows affected rows and time for all queries Show notifications for slow queries and queries with errors Filter queries by query type (SELECT, UPDATE, DELETE, etc) Filter ...


2

A key must be defined for a table by using a single column, or multiple. So on your code, you need to a line to the sql KEY id (id) - $sql = "CREATE TABLE $table_name ( id int NOT NULL AUTO_INCREMENT, feedurl text NOT NULL, category text NOT NULL, KEY id (id) );";


2

dbDelta is squirrelly. Some things it will alter, others it will not. For example, and if I remember correctly, you can add indexes to tables but not change those indexes or delete them. I honestly don't know if it will remove table columns. (My guess it that it won't but I may test that later.) Nonetheless, I would not do something as critical as add ...


2

Here's what Codex has to say on restricting database user privileges: For normal WordPress operations, such as posting blog posts, uploading media files, posting comments, creating new WordPress users and installing WordPress plugins, the MySQL database user only needs data read and data write privileges to the MySQL database; SELECT, INSERT, UPDATE and ...


2

From the Codex page for $wpdb: The function [$wpdb->query] returns an integer corresponding to the number of rows affected/selected. If there is a MySQL error, the function will return FALSE. So in order to display a success/failure message, it should be a simple matter: $result = $wpdb->query( " DELETE FROM $wpdb->posts WHERE ...


2

The behavior I described in my post only happens when you are using $wpdb->update to execute an UPDATE statement. If you want to update a different database using the same connection, use $wpdb->query to send a raw query, WP won't modify it then.


2

To answer your exact questions: Yes. Just use MariaDB instead of MySQL. No, it is a straight replacement. The exact details depend heavily upon your exact situation. However, MariaDB is marketed as a compatible MySQL replacement. AFAIK, it works with WordPress with no changes at all.


2

All options with autoload = yes (default) are fetched very early in one query. So the number of options does affect performance only marginally. Split options if you don’t need everything on every page load. With … add_option( 'option_name', 'option_value', '', 'no' ); … you can set options that aren’t loaded before you actually call get_option().



Only top voted, non community-wiki answers of a minimum length are eligible