简体   繁体   English

如何更新现有自定义帖子类型的类别?

[英]How to update the categories of existing custom post types?

I am picking up work on a legacy tool and am redoing a page that works with custom post types. 我正在使用旧版工具进行工作,并正在重做适用于自定义帖子类型的页面。 The new post type we'll call new_resource , and has the same "category" identifiers as the existing legacy resource type, old_resource . 新的帖子类型称为new_resource ,并且具有与现有旧资源类型old_resource相同的“类别”标识符。 I'm pretty new to Wordpress, so there may have been a better way of doing that than just starting from scratch, but this is where I'm at. 我对Wordpress还是很陌生,所以可能有比从头开始更好的方法,但这就是我要的。 My goal is to use the same data that the existing resources used, so I don't have to create all of the new posts. 我的目标是使用与现有资源所使用的数据相同的数据,因此不必创建所有新帖子。

I ran two queries that updated the existing resource types to the new one: 我运行了两个查询,将现有资源类型更新为新的:

update wp_posts set post_type = 'new_resource' where post_type = 'old_resource';

and

update wp_posts set guid = replace(guid, 'old-resource', 'new-resource');

All of that is working. 所有这些都在起作用。

What I want to do now is update the custom taxonomy (I'm not using the default categories built in). 我现在想做的是更新自定义分类法(我没有使用内置的默认类别)。 We'll call it resource-category for the example. 在示例中,我们将其称为资源类别 The existing old-resource posts all had their resource-category assigned, and when I query wp_terms, I can see all of the categories. 现有的旧资源帖子都分配了资源类别,当我查询wp_terms时,我可以看到所有类别。 I've set up those SAME categories in the new-resource type. 我已经在new-resource类型中设置了这些SAME类别。 Is there a query that I can run that will select all of the resources with a type of new-resources (all of them are now, after I ran that update query above), and set their categories? 我是否可以运行一个查询,该查询将选择具有新资源类型的所有资源(在我运行上述更新查询后,所有资源现在都已设置)并设置其类别? Like update their term_id where the term_id is still pointing to an ID of the old category type? 像更新他们的term_id一样,其中term_id仍指向旧类别类型的ID?

Wordpress Version: 4.2.2 PHP Version: 5.3.3 WordPress版本:4.2.2 PHP版本:5.3.3

Edit: 编辑:

I ran this query: 我跑了这个查询:

mysql> select term_id, name, slug from wp_terms where slug = 'videos';

+---------+--------+--------+
| term_id | name   | slug   |
+---------+--------+--------+
|      69 | Videos | videos |
|     177 | Videos | videos |
+---------+--------+--------+
2 rows in set (0.00 sec)

The term_id 69 belongs to the old resource type. term_id 69属于旧资源类型。 The term_id 177 belongs to the new resource. term_id 177属于新资源。 I want all of my current resources (the ones I already ran the update resource type query on) to point to term_id 177 instead of 69, but I'm not sure of the relationship between posts and terms. 我希望所有当前资源(已经在其上运行更新资源类型查询的资源)都指向term_id 177而不是69,但是我不确定帖子和术语之间的关系。

Something like update the term_id for all new-resources, set 69 to 177. 诸如更新所有新资源的term_id之类的东西,将69设置为177。

The update set only the column in set clause .. and i set clause ypu can have more than a column 更新只设置了set子句..和i set子句ypu中的列

You could do the two update using an update only 您可以仅使用更新来进行两次更新

 update wp_posts 
         set post_type = 'new_resource', 
         guid = replace(guid, 'old-resource', 'new-resource')
 where post_type = 'old_resource';

I figured it out. 我想到了。 I ran a query that selected all of the info I'm comparing: 我运行了一个查询,该查询选择了我正在比较的所有信息:

SELECT p.post_title as “wp_post_title”, tr.term_taxonomy_id as “term_rship_id”, tt.taxonomy as “term_tax_name”, tm.slug as “terms_slug”
FROM wp_posts p
INNER JOIN wp_term_relationships tr ON p.ID = tr.object_id
INNER JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms tm ON tm.term_id = tt.term_id
WHERE tt.taxonomy = 'exp_wp_resource_category' or tt.taxonomy = 'resource-categories';

Then I updated my existing taxonomy_ids in the wp_term_relationship table. 然后,我在wp_term_relationship表中更新了现有的taxonomy_id。

UPDATE wp_term_relationships SET term_taxonomy_id = 176 WHERE term_taxonomy_id = 13;

13 is the ID of the old-resource video type, and 175 is the ID of the new-resource video type. 13是旧资源视频类型的ID,而175是新资源视频类型的ID。

Just took some digging through the Wordpress table relationships. 只是仔细研究了Wordpress表之间的关系。

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

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