Tag Info

Hot answers tagged

35

Hi @Keith Donegan: If I understand your question correctly I think this is what you are looking for? <?php echo $GLOBALS['wp_query']->request; ?> $wp_query is a global variable that contains the current query run by the loop. If you run the above code anytime while the loop is still active or even right after the loop it should give you the SQL ...


19

Post meta information is automatically cached in memory for a standard WP_Query (and the main query), unless you specifically tell it not to do so by using the update_post_meta_cache parameter. Therefore, you should not be writing your own queries for this. How the meta caching works for normal queries: If the update_post_meta_cache parameter to the ...


16

Here is a modified checklist, based on my current (work-in-progress) settings/data security checklist used for reviewing Themes (the principles should be no different for Plugins than they are for Themes): Plugins should prefix all options, custom functions, custom variables, and custom constants with plugin-slug. Plugins should implement Plugin Options ...


15

I would solve this with a filter on WP_Query. One that detects an extra query variable and uses that as the prefix of the title. add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 ); function wpse18703_posts_where( $where, &$wp_query ) { global $wpdb; if ( $wpse18703_title = $wp_query->get( 'wpse18703_title' ) ) { $where .= ' ...


11

There are two aspects to this: Basic principles. Whatever is written into database should be checked for SQL injects. Whatever is printed to screen should be checked that it doesn't print harmful JavaScript. Whenever someone does something it should be checked that it was his intention to do so and he has appropriate capability. There are many many more ...


10

The like_escape() function exists in WordPress because the regular database escaping does not escape % and _ characters. This means you can add them in your arguments to wpdb::prepare() without problem. This is also what I see in the core WordPress code: $wpdb->prepare(" AND $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND ...


8

Ok, I got there at the end. I couldn't use WP_Query class as I really needed to have my own pretty big and complex SQL. Here is what I ended up having: In functions.php I have my custom SQL and logic for counting the values needed for the WP pagination logic: function vacancies_current( ){ global $wpdb, $paged, $max_num_pages, $current_date; ...


7

Best to do options, posts, post content and post meta: UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com'); UPDATE wp_posts SET post_content = ...


6

I can't use $wpdb->prepare, since I want to be able to add variables to my query string that look something like: $var = "AND pm.meta_value = '%$_POST['val']%'"; To get a literal % to pass through $wpdb->prepare just double it. You don't need to be avoiding $wpdb->prepare. Proof of concept: var_dump($wpdb->prepare('SELECT * FROM ...


5

A simple user meta row can handle that for you (the second issue), you can store the post id and the vote (up/down) in an array and that is just the same as post meta ex /** * update user vote per post * @param int $user_id * @param int $post_id * @param mixed $vote can be an integer 1 / -1 and can also be a string "up"/"down" * @return void */ ...


5

Use mysqli_real_escape_string(). The core uses still the deprecated mysql_real_escape_string() or add_slashes() in wpdb::_real_escape() … /** * Real escape, using mysql_real_escape_string() or addslashes() * * @see mysql_real_escape_string() * @see addslashes() * @since 2.8.0 * @access private * * @param string $string to escape * @return ...


5

I would recommend a pivot query. Using your example: SELECT p.ID, p.post_title, MAX(CASE WHEN wp_postmeta.meta_key = 'first_field' then wp_postmeta.meta_value ELSE NULL END) as first_field, MAX(CASE WHEN wp_postmeta.meta_key = 'second_field' then wp_postmeta.meta_value ELSE NULL END) as second_field, MAX(CASE WHEN ...


5

The table where your URL is saved is wp_options. You should do an update on the columns that use the URL for your site: UPDATE TABLE wp_options SET option_value = "new domain" WHERE option_name = "siteurl" UPDATE TABLE wp_options SET option_value = "new domain" WHERE option_name = "home" I might be missing some value, but whenever you do this find/replace ...


5

See this answer: Best Collection of Code for your functions.php file Then add ?debug=sql to any WP URL, and it'll output the full list of queries that were run. (And yes, it's scary...)


5

You should do defaults at the time of pulling the data out. Never insert default values into the database. Defaults are default. Options in the DB override defaults. How to do defaults for a serialized options array: $defaults = array( 'default1' => '1', 'default2' => '2', ); $options = wp_parse_args(get_option('plugin_options'), $defaults);


4

If it is an older version of WP, you need to find out what version of WP generated the database, as WP upgrades the DB most times the files are upgraded and you should incrementally upgrade to also upgrade the database. Look in the wp_options table for option 711 and see what the version is; option name will be site_transient_update_core and the value will ...


4

Rather than constructing query from scratch, it is easier to see what exactly is WordPress querying when API function is used: get_posts(array( 'numberposts' => -1, )); var_dump( $wpdb->last_query ); Gives following SQL: SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = ...


4

<?php /** * Plugin Name: Delete Non Authors */ function delete_non_authors() { global $wpdb; $non_authors = $wpdb->get_col( "SELECT DISTINCT $wpdb->users.ID FROM $wpdb->users LEFT JOIN $wpdb->posts ON $wpdb->users.ID = $wpdb->posts.post_author WHERE $wpdb->posts.ID IS NULL" ); foreach ...


4

All you need to do is modify the SQL query. Using the code you linked as a base: $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND year(post_date) = 2010"); if (0 < $numposts) $numposts = number_format($numposts); The 'AND' I added basically gives you all the posts of 2010. Change the year ...


4

As Ben suggested, you need to pass the connection details when creating the wpdb class: $newdb = new wpdb( 'user', 'password', 'database', 'hostname' ); You should also test that the query actually returned something before using the result in a foreach loop: if ($rows) { foreach ($rows as $obj) { ... } }


3

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} ...


3

The WordPress database is already indexed. See this codex article for a detailed list of indexes per table: http://codex.wordpress.org/Database_Description And even if it weren't, you'd need to know what queries are being run in order to effectively add indexes. Meaning, there would be no quick fix--you'd have to learn how indexing works, figure out what ...


3

$wpdb is not suit for fetching huge amount of data from database. Why? In your case: $wpdb->get_results( ... ) - fetches all results into your RAM at once. It means if you have 4mb, 10mb, or 50mb of data in db, everything will be stored in memory (what is limited as you know). $wpdb->get_results( ..., ARRAY_A ) - $wpdb fetches everything as object by ...


3

Yes. Use WP_HOME and WP_SITEURL in your wp-config.php, so the URLs in the database won't mess (a lot) with local site development. define ('WP_HOME', 'http://local/site/url'); define ('WP_SITEURL', 'http://local/site/url'); Also, some other good practices: Put in your .gitignore things like: wp-config.php wp-content/uploads wp-content/cache wp-content ...


3

You just have to modify your query to get only first 25 matches SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1' limit 25. The result is already order by id what is incremented automatically, you don't have anything to bother about there. Also please note, it is not a good practice to do such queries on ...


3

First of all i wouldn't use phpmyadmin for a database that large. Use something like Sequel Pro its free and easy to use. Second if you dont want the tables that WPML Created and you have removed the plugin from your site you can remove the tables that it creates. Upload the sql to a new database on your local machine and remove all this tables for each ...


3

The error in the SQL statement is at the very end, SELECT * FROM c_posts WHERE id = , There is no value being supplied to check against the id column. This is most likely because you didn't check for a blank value in the ID field before forming and executing the query. Check the code in the function apt_publish_post to ensure that the value being ...


3

Tax Query Limits A Taxonomy Query in WordPress supports the following three arguments for the operator parameter: IN, NOT IN and AND. So it basically can't do what you're trying to do. Not even with advanced (tax_query) Queries. Meta Query and possibilities You'll want to move your concept a bit and work with post meta fields/data. The WP_Query uses the ...


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

My sites on one account all got infected with a Decode_Base64 script that infected many php files, and despite cleaning up a site which took hours, it got re-infected just hours later. I ended up downloading the wp-content/uploads folder and any other manually updated files using a secure ftp connection. I also took notes/backups of the themes, plugins, ...



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