过期域名预定抢注

 找回密碼
 免费注册

wordpress批量更新category的方法

  [複製鏈接]
發表於 2013-7-9 10:58:26 | 顯示全部樓層 |閱讀模式
wordpress在線寫英文有些慢,我是在本地寫了一個簡單的錄入系統.寫好後批量導入進去.這牽涉到一些category或者tag的導入.為了批量導入.寫了一個存儲過程.然後批量調用存儲過程,可以把category導入進去.分享一下.# Z$ z5 _8 k8 ]0 u  `

& M  T  R  V* Z# C4 g. k4 n0 F' K' w8 X0 \- L
CREATE  PROCEDURE `p_add_article_category`(in v_term VARCHAR(300),in v_title varchar(2000))+ c, `* u2 ~- b' Y; `: C( {
BEGIN
# g: W- Z( D: P- iset @wp_post_id=IFNULL((select id from wp_posts where post_title=v_title limit 0,1),0);6 F7 Z" C% J: O" _
if (@wp_post_id>0) then
# Q, n! b# y1 m7 E% e   set @term_id=IFNULL((select term_id from wp_terms where name=v_term limit 0,1),0);
2 N2 y. x3 @0 ?: I# l   if (@term_id=0) then
4 \) k" p8 Y1 X2 N9 j- x7 G# {       insert into wp_terms(name,slug, term_group) values(v_term,v_term,0);1 I- `1 e3 b3 S0 u1 Y( |
       set @term_id=(select term_id from wp_terms where name=v_term limit 0,1);% p2 }2 d' I7 n" f: X
   end if;9 `# F( W( o) G: d$ N% z
   set @term_taxonomy_id=IFNULL((select term_taxonomy_id from wp_term_taxonomy where taxonomy='category' and term_id=@term_id limit 0,1),0);
, g( M( e, q6 C" f( {   if (@term_taxonomy_id =0) then
3 \( U! P' l( @, ]1 V: o6 x% A5 ~           insert into wp_term_taxonomy(term_id,taxonomy,description,parent,count) values(@term_id,'category','',0,0);
! D! h# P, X$ |" {/ Y% J& `           set @term_taxonomy_id=(select term_taxonomy_id from wp_term_taxonomy where taxonomy='category' and term_id=@term_id limit 0,1);
4 b7 ?0 A/ d. \9 }: `: }* b   end if;- H$ J/ t/ o8 H2 I/ d  l& X
' M( D- V  x4 O$ V6 L9 d5 q+ {
   if(not EXISTS(select 1 from wp_term_relationships where object_id=@wp_post_id and term_taxonomy_id=@term_taxonomy_id)) then5 O5 n% ?' q0 u$ A* T2 J9 F' y
           insert into wp_term_relationships(object_id,term_taxonomy_id,term_order) values(@wp_post_id,@term_taxonomy_id,0);
3 O6 ?  {+ T/ i0 @           update wp_term_taxonomy set count=count+1 where taxonomy='category' and term_id=@term_id;6 b7 m; Y; P/ U$ E! ~' \2 P
   end if;2 B& I# S- [  j; _6 {) t9 t2 B
end if;
, ]5 H) W( ?7 V2 O* i& Y6 ]END  A) o" B6 V9 @. ?3 Q
: Q; C" c$ I. z9 x1 h, ?0 [6 N! d% r
調用方法:
  x9 Q8 i" E0 `5 c$ P* R1 N$ m- S  X  M4 s
call  p_add_article_category('scenery','this is the title about scenery');% m" b, `; b$ `2 H; }1 }: y
8 V: N& x4 h* `3 S- {: P* A
& A& \3 p) O1 _6 Z$ i8 C4 s

評分

參與人數 2點點 +4 收起 理由
月光飞燕 + 3
sophieqd + 1

查看全部評分

發表於 2013-7-9 11:19:57 | 顯示全部樓層
火車必用,謝謝分享。好東西
% [2 p" ]0 B- v: \& \: u
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-9 21:11:13 | 顯示全部樓層
好東西,謝謝分享!愛瘋!
" L% D5 n( x0 |3 x+ g8 [8 I
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 02:41:54 | 顯示全部樓層
能詳細說下怎麼用嗎?5 N1 ~& Z3 x; ~  }/ v
我是不是建一個php文件,把這個句子寫進去
+ z! ?6 @1 U' Q$ I( U/ A. U, ]  ?& ^比如
1 ^7 u: B+ J' ucall  p_add_article_category('scenery','this is the title about scenery');
8 S! i6 `! ^/ E4 X9 Y" Mcall  p_add_article_category('scenery','this is the title about scenery 2');, x/ A% y& a) V4 A
call  p_add_article_category('scenery','this is the title about scenery 3');, Y" [0 c9 T! @
這樣是添加3個文章到目錄了.
& d! T$ O' p: K/ I. [如果一次改n條,是不是就要寫n條.
3 \6 G/ z6 }" e# ]8 o可以用關鍵字匹配分類嗎,比如標題有scenery的都一次分類到scenery去
4 r- g0 A5 {$ c
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 10:00:03 | 顯示全部樓層
還在搞自動阿,太佩服了。1 u' ?: I! ^" ?: N$ v
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 08:58:33 | 顯示全部樓層
有魚不換酒 發表於 2013-7-10 02:41
/ p0 {* R8 V/ N能詳細說下怎麼用嗎?. J+ m; w3 y4 s! t
我是不是建一個php文件,把這個句子寫進去
$ L. T" T) f, Z: V0 L* U1 I2 {3 Z: y比如
. [. r; f. Y6 q& ~2 p
先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.
1 |* Y. N% x5 Y4 `. _6 }
# N- q; s2 A& |  q) A- Q8 p  I
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-11 16:03:57 | 顯示全部樓層
shanguiyao 發表於 2013-7-11 08:58 , V6 G# S( A2 U
先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.& n: Z2 J9 r, C" \0 Z3 s
( b7 ~, Q$ F0 G* \* F
...

, g& K" V! S/ ~  s$ I( m每一個post的標題都是不一樣的呀.這樣不是要為每一個post文章調用一次你這個存儲過程嗎?3 B1 a4 W. o7 v1 \1 j
批量調用一次,不所有包含某個關鍵字標題的文章都添加一個分類去.這樣如何實現呢
0 i' S; j6 d0 w) ]# B) E
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 20:20:41 | 顯示全部樓層
對,每一個post標題調用一次.可以批量生成的吧?寫個sql都拼出來要執行的一堆存儲過程語句了.應該不太難吧.
6 I* e2 |, S# s- i9 G" u8 M
回復 给力 爆菊

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 免费注册

本版積分規則

點基跨境 數位編輯創業論壇

GMT+8, 2025-7-11 12:44

By DZ X3.5

小黑屋

快速回復 返回頂部 返回列表