过期域名预定抢注

 找回密碼
 免费注册

wordpress批量更新category的方法

  [複製鏈接]
發表於 2013-7-9 10:58:26 | 顯示全部樓層 |閱讀模式
wordpress在線寫英文有些慢,我是在本地寫了一個簡單的錄入系統.寫好後批量導入進去.這牽涉到一些category或者tag的導入.為了批量導入.寫了一個存儲過程.然後批量調用存儲過程,可以把category導入進去.分享一下.
4 s3 g) b$ l' o" n8 R- p
# \0 t7 a' K; w2 n% y0 @3 e8 J2 D: w+ z5 a2 \& [
CREATE  PROCEDURE `p_add_article_category`(in v_term VARCHAR(300),in v_title varchar(2000))
+ e( g  k4 M  \+ K( h4 ABEGIN" s% Z: z9 O) }: c
set @wp_post_id=IFNULL((select id from wp_posts where post_title=v_title limit 0,1),0);
- ?. C& d4 P( o4 v+ O/ @if (@wp_post_id>0) then
- \1 x, C' ?# o$ b4 `$ J1 e   set @term_id=IFNULL((select term_id from wp_terms where name=v_term limit 0,1),0);
8 ?* z1 X% T& A3 a   if (@term_id=0) then+ m7 I0 f5 e4 S4 Q' i& q; g$ |
       insert into wp_terms(name,slug, term_group) values(v_term,v_term,0);
! E1 V* u. c" ]2 U" r       set @term_id=(select term_id from wp_terms where name=v_term limit 0,1);
! Q$ `9 U3 k9 L# o+ X   end if;
$ A. |, E. a7 j4 n   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);
2 e" f  F6 m) h4 ], i  B8 A   if (@term_taxonomy_id =0) then0 E' P$ C+ N0 F, \, i7 v2 {  U$ U* Q
           insert into wp_term_taxonomy(term_id,taxonomy,description,parent,count) values(@term_id,'category','',0,0);. c& I0 m" n- V6 s5 U
           set @term_taxonomy_id=(select term_taxonomy_id from wp_term_taxonomy where taxonomy='category' and term_id=@term_id limit 0,1);
' Y. ]9 c9 v5 a5 A- p0 ~6 `   end if;
5 J% [3 z! y# o7 Z
/ Q- @3 Q0 b9 M9 B* h- D   if(not EXISTS(select 1 from wp_term_relationships where object_id=@wp_post_id and term_taxonomy_id=@term_taxonomy_id)) then
+ B. c9 @7 I" P( b8 b8 J           insert into wp_term_relationships(object_id,term_taxonomy_id,term_order) values(@wp_post_id,@term_taxonomy_id,0);
& Z" R# {( h& Y           update wp_term_taxonomy set count=count+1 where taxonomy='category' and term_id=@term_id;
% X' C( A3 r  E, U) q0 l8 l; Q. D   end if;$ w. M& J3 @( o. k# U1 L( j* f
end if;4 l$ b7 l3 q8 w1 H4 l* Q2 B
END: c4 b- ~) A! e0 j% c
; F* {( r; u* s, U6 _8 a/ @
調用方法:$ G! c3 n# y: a/ o8 Q. {" }
8 V8 v" Q1 Q- r) Q2 X
call  p_add_article_category('scenery','this is the title about scenery');
0 x2 F8 m. s+ b0 J3 f! M  b
% N# {; F# y# X% |: }% ^. {! p7 _' m# R. D; ^  p

評分

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

查看全部評分

發表於 2013-7-9 11:19:57 | 顯示全部樓層
火車必用,謝謝分享。好東西
/ c8 E. A$ ^/ E: Y( e6 z
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-9 21:11:13 | 顯示全部樓層
好東西,謝謝分享!愛瘋!
# b4 e( @1 ]& M$ d1 x+ P! i- e* t
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 02:41:54 | 顯示全部樓層
能詳細說下怎麼用嗎?
7 @, `& J* m3 L" ]3 Q我是不是建一個php文件,把這個句子寫進去
* k$ E8 @1 s8 f8 I比如
4 ^2 H) W$ p2 y2 P' ecall  p_add_article_category('scenery','this is the title about scenery');
' x2 K1 N1 s+ \' e+ k6 dcall  p_add_article_category('scenery','this is the title about scenery 2');. O  }2 B, \0 v" H
call  p_add_article_category('scenery','this is the title about scenery 3');
8 z; ~2 g: l5 E7 Y' K6 z0 P這樣是添加3個文章到目錄了.
4 a% T, B- I  q1 E如果一次改n條,是不是就要寫n條.
9 a" z8 v) @+ s4 b1 K" K可以用關鍵字匹配分類嗎,比如標題有scenery的都一次分類到scenery去
. j) f  n6 F; `% f  h; H# Q
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 10:00:03 | 顯示全部樓層
還在搞自動阿,太佩服了。
8 o! w; e6 \& G. y( A0 V
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 08:58:33 | 顯示全部樓層
有魚不換酒 發表於 2013-7-10 02:41
. {+ S# @2 {1 z, W! d能詳細說下怎麼用嗎?
5 t: b) E# P5 e, ~+ u. Y/ }我是不是建一個php文件,把這個句子寫進去0 Q9 U/ `1 J6 j8 K" B
比如

5 z1 m4 s; I; p2 L* y0 S先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.
2 d* z% n6 n8 y% p9 r) g; U
9 j4 M3 J7 \( c! ]
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-11 16:03:57 | 顯示全部樓層
shanguiyao 發表於 2013-7-11 08:58
, h2 Z7 y. H2 D8 E5 ?先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.
" Z7 E$ e5 I) B  v7 \
3 p; j/ z/ Z; f: c3 ? ...

9 Z( z2 j$ l7 y" I每一個post的標題都是不一樣的呀.這樣不是要為每一個post文章調用一次你這個存儲過程嗎?
8 _8 v) k" w% i# s" I2 Y8 C批量調用一次,不所有包含某個關鍵字標題的文章都添加一個分類去.這樣如何實現呢$ y- Y4 M1 K3 A  P
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 20:20:41 | 顯示全部樓層
對,每一個post標題調用一次.可以批量生成的吧?寫個sql都拼出來要執行的一堆存儲過程語句了.應該不太難吧.
& ]3 H' X* ]1 y7 S7 f- p$ l8 _
回復 给力 爆菊

使用道具 舉報

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

本版積分規則

过期高净值品牌域名预定抢注

點基

GMT+8, 2026-6-26 15:27

By DZ X3.5

小黑屋

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