MySQL查询,查找带有自定义字段的帖子,然后更改类别

时间:2011-10-30 作者:Tom

我正在寻找一个MySQL查询,该查询将查找具有自定义字段“meta\\u key\\u I\\u want\\u to\\u find”的所有帖子,然后如果检测到自定义字段,则向该帖子添加一个类别。类别已经创建,所以我只需要找到带有该Meta\\u键的所有帖子,然后将类别添加到其中。

我想的是这样的:

    <?php
    $username="MY_MYSQL_USERNAME";
    $password="MY_MYSQL_PASSWORD";
    $hostname="MY_DATABASE_HOSTNAME"; //Your Database hostname usually
    $database="MY_DATABASE_NAME";
    $my_text = "meta_key_I_want_to_find";  // What I\'m searching for
    $my_category = \'8\';    // The category to add it to

    // Connect to MySQL and the database and verify:
    mysql_connect($hostname,$username,$password) or die(mysql_error());

    echo "<p>Connected to MySQL.";
    mysql_select_db($database) or die(mysql_error());
    echo "<br />Connected to " . $database . "</p>";

    // Verify what we\'re looking for, for troubleshooting:
    echo "<p><b>Looking for " . $my_text . "</b></p>";

    // Get the ID field (which is WordPress\'s post
    // number) from any posts that have your text:
    $query = "SELECT ID FROM wp_posts WHERE post_content LIKE \'%$my_text%\'"; 

    // Take those results and go through them:
    $result = mysql_query($query) or die(mysql_error());

    // While there are results...
    while($row = mysql_fetch_array($result))
    {
    // Verify what we\'re doing -- changing post
    // number such-and-such...
    $thisPostHasIt = $row[\'ID\'];
    echo "<p>Row " . $row[\'ID\'] . " contains it, so...<br />";

    // In the wp_term_relationships table,
    // update the category number ("term_taxonomy_id")
    // with the category number you specified -- but only
    // in one of the "result" rows.
    // We look for "object_id" to equal one of those
    // rows. (The object_id field refers to the WordPress
    // post number, just as the ID field did. Why two
    // different names? Who knows?)

    mysql_query("UPDATE wp_term_relationships SET term_taxonomy_id=\'$my_category\' WHERE object_id = \'$thisPostHasIt\'");

    // And tell us about it:
    echo "Changing post number " . $thisPostHasIt . " to category number ". $my_category . "</p>";
    }
    echo "<p><b>All done!</b></p>";

//found on http://wordpress.org/support/topic/mysql-query-to-search-posts-and-change-categories
?>
。。。我只需要能够找到具有特定meta\\u键的所有帖子,然后向其中添加一个类别,而不是查找字符串并替换它。

非常感谢您的任何建议!

1 个回复
最合适的回答,由SO网友:radgeek 整理而成

我无法判断您是否有可用的WordPress API。(这段代码是从WordPress实例中运行的,还是由WordPress数据库上的外部脚本运行的,而没有引导WordPress本身?)

如果您有WordPress API,那么很容易获得具有给定键的postETA值的所有帖子的列表。因此:

<?php
$q = new WP_Query(array(
    "meta_key" => "meta_key_i_want_to_find"
));
while ($q->have_posts()) : $q->the_post();
     // Do something here. E.g.: $posts[] = get_the_ID(); to collect IDs
endwhile;
wp_reset_query();

// If you just collected above, now do something with it...
同样,如果您有可用的WordPress API,您可以使用wp_set_post_terms().

如果您没有可用的WordPress API,可以使用以下方式获取所需post数据的表示:

-- Modify to SELECT all and only the fields you intend to use.
SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON 
wp_posts.ID=wp_postmeta.post_ID WHERE (meta_key=\'meta_key_i_want_to_find\');
在ID中循环,同时手动添加类别关系。

结束

相关推荐

帮助运行MySQL查询以更新多站点安装中的所有wp_#_options表

我想通过我的WordPress数据库运行一个查询,更新所有单个站点上的blog\\u公共值。我一直在思考如何让语句查找wp\\U 2\\U选项、wp\\U 3\\U选项等。我可以单独做这样的事情:UPDATE wp_10_options SET option_value = REPLACE(option_value, \'1\', \'0\') 但我一直在思考如何运行一个可以遍历所有表的查询。有什么帮助吗?提前感谢:)