简体   繁体   English

在预先存在的Wordpress插件中运行自定义SQL

[英]Running custom SQL in pre-existing Wordpress Plugin

OK, so I'm a bit of a novice when it comes to PHP, but here's what I want to achieve. 好的,关于PHP我还是个新手,但这就是我想要实现的目标。

I want to change the theme of a Wordpress site from one that uses a plugin to rate the posts (WP-Ratings) to a new theme where ratings are built in. The new theme stores the ratings as meta data, and I have managed to write a query which takes the rating from the wp_ratings table and converts it into the necessary meta data. 我想将Wordpress网站的主题从使用插件对帖子(WP-Ratings)进行评分的网站更改为内置评分的新主题。新主题将评分存储为元数据,并且我设法编写一个查询,该查询从wp_ratings表中获取评级并将其转换为必要的元数据。 What I would like to do, is store this query in the current ratings plugin, so that any time a post is rated is stores the necessary meta data for when we migrate to the new theme. 我想做的是将此查询存储在当前的评级插件中,以便在对帖子进行评级时,每次存储迁移到新主题时所需的元数据。 Like I said, I am a novice to PHP, but can hold my own with SQL, so could someone please advise me how to run the following query against a Wordpress database. 就像我说的那样,我是PHP的新手,但是可以使用SQL,因此有人可以建议我如何对Wordpress数据库运行以下查询。

Thanks! 谢谢!

SET @post_id = 1110;
SET SESSION group_concat_max_len = 1000000;

insert into wp_postmeta (post_id,meta_key,meta_value)
  SELECT
    rating_postid
    ,'like'
    ,CONCAT
      ('a:', COUNT(rating_id), ':{',
        (SELECT CONCAT( GROUP_CONCAT(meta_data_vote SEPARATOR ''), '}') 
         FROM
          (SELECT CONCAT
            ('i:',
             @curRow := @curRow + 1,
             ';a:2:{s:7:"',
             'user_id',
             '";s:1:"0";s:2:"ip";s:16:"',
             '-127-000-000-001',
             '";}'
            ) AS meta_data_vote
           FROM wp_ratings JOIN
             (SELECT @curRow := -1 AS j) r
              WHERE rating_postid = @post_id
                and rating_rating > 0
             )AS meta_data_votes
          )
      ) AS new_ratings_meta_data
  FROM wp_ratings l
  WHERE rating_postid = @post_id
    and rating_rating > 0

Please note that the post ID variable should equal the post ID of the one being rated, which I believe is $post_id in PHP for Wordpress. 请注意,帖子ID变量应等于被评级的帖子的ID,我认为在PHP for Wordpress中,该帖子的ID为$ post_id。

You want to use the wpdb . 您要使用wpdb It is a very simple wrapper around PHP's mysql (yes, mysql ) functions. 它是PHP的mysql函数(是mysql )的非常简单的包装。

$results = $wpdb->query($your_complicated query); // Congrats on actually knowing how to write SQL :)

You can feed any valid SQL into that methaod and it should work just like any query but you can't "stack" queries. 您可以将任何有效的SQL送入该方法,它应像任何查询一样工作,但不能“堆叠”查询。 mysql and PDO do allow stacked queries (though I am not really a fan of them, being paranoid and all), but, as mentioned wpdb still uses mysql functions. mysqlPDO确实允许堆栈查询(尽管我并不是真正的粉丝,但是偏执狂和所有),但是,如前所述, wpdb仍然使用mysql函数。

You should also replace your hard-coded WordPress table names with the $wpdb equivalents. 您还应该用$wpdb等效项替换硬编码的WordPress表名。 For example, instead of wp_posts use $wpdb->posts . 例如,使用$wpdb->posts代替wp_posts The reason for this is that wp_ prefix is changable. 原因是wp_前缀是可更改的。 It is the default and it very common but can be changed, and changed easily. 它是默认值,非常常见,但是可以更改,并且可以轻松更改。 It is just a simple edit to wp-config.php . 这只是对wp-config.php的简单编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM