过期域名预定抢注

 找回密碼
 免费注册

wordpress批量更新category的方法

  [複製鏈接]
發表於 2013-7-9 10:58:26 | 顯示全部樓層 |閱讀模式
wordpress在線寫英文有些慢,我是在本地寫了一個簡單的錄入系統.寫好後批量導入進去.這牽涉到一些category或者tag的導入.為了批量導入.寫了一個存儲過程.然後批量調用存儲過程,可以把category導入進去.分享一下.3 n" u, \( f1 x7 k  ?
8 S2 T4 d- z6 n- z8 s: x3 e
2 e1 R! B: z2 r- t4 E1 R
CREATE  PROCEDURE `p_add_article_category`(in v_term VARCHAR(300),in v_title varchar(2000))' E/ k  k* O) n! k
BEGIN
/ ^- X" p- @! Vset @wp_post_id=IFNULL((select id from wp_posts where post_title=v_title limit 0,1),0);
' j# I8 J% u8 ^; L8 q' |8 kif (@wp_post_id>0) then
: O1 P" U: |9 b  P3 q. v0 L   set @term_id=IFNULL((select term_id from wp_terms where name=v_term limit 0,1),0);
  Y8 \( g; }$ L1 ?0 i' M   if (@term_id=0) then
3 }7 X  N0 m, ?" I- P       insert into wp_terms(name,slug, term_group) values(v_term,v_term,0);& ]1 \( G6 c; }- _2 i  G2 C* D
       set @term_id=(select term_id from wp_terms where name=v_term limit 0,1);8 A! s! |" B5 j, s, g9 o5 b
   end if;
, t4 p; F* l- R5 t9 f8 E   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);
6 }4 d5 [2 ]4 ^   if (@term_taxonomy_id =0) then
% ?, O# L5 X1 m7 o           insert into wp_term_taxonomy(term_id,taxonomy,description,parent,count) values(@term_id,'category','',0,0);+ h! k7 m( G* d9 K+ m( J2 P( Y
           set @term_taxonomy_id=(select term_taxonomy_id from wp_term_taxonomy where taxonomy='category' and term_id=@term_id limit 0,1);' `, C0 H, N5 a# f" ?
   end if;# ^7 E) ?  M- Q7 ?6 X( u
  r: Z2 r7 A. T" J7 M0 _
   if(not EXISTS(select 1 from wp_term_relationships where object_id=@wp_post_id and term_taxonomy_id=@term_taxonomy_id)) then
# g0 h& I! {9 c           insert into wp_term_relationships(object_id,term_taxonomy_id,term_order) values(@wp_post_id,@term_taxonomy_id,0);
: Z( S9 }) E( O8 o( t           update wp_term_taxonomy set count=count+1 where taxonomy='category' and term_id=@term_id;
/ }0 O7 U' B) T   end if;
. @5 Z0 z7 t! j" P- `% L6 J: eend if;
- Q& p8 F7 r0 y3 E# w, \' WEND4 L/ @% h5 R, Y2 i1 k( f# t
& a$ z& s+ i8 x! H( x# I: q' S3 t% o
調用方法:% O! j; A4 @4 a2 B3 g
, K$ x% O- e* s( S  [4 g1 }- Z( v. q
call  p_add_article_category('scenery','this is the title about scenery');& p: V0 o5 t2 O$ I

' H0 G, V3 s0 I2 s% k  \" L: X+ P
) a5 Z* Z1 m6 {; m

評分

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

查看全部評分

發表於 2013-7-9 11:19:57 | 顯示全部樓層
火車必用,謝謝分享。好東西6 {# A- y$ ^* q5 Z6 K; g$ M# s% D
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-9 21:11:13 | 顯示全部樓層
好東西,謝謝分享!愛瘋!. X5 M0 ^, e$ m+ T+ \
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 02:41:54 | 顯示全部樓層
能詳細說下怎麼用嗎?
0 d; V) s& ^/ a0 F我是不是建一個php文件,把這個句子寫進去7 x3 `5 N- f8 F
比如
# |$ g! G% G; q6 V- {& Ocall  p_add_article_category('scenery','this is the title about scenery');% A/ H, b4 _1 v! @
call  p_add_article_category('scenery','this is the title about scenery 2');
6 W: _, K6 d! a: G( C3 ?/ f% \call  p_add_article_category('scenery','this is the title about scenery 3');! m1 i2 P# D+ O/ D3 @8 @
這樣是添加3個文章到目錄了.
( C9 T% `3 S: Z, ~- [4 ^如果一次改n條,是不是就要寫n條.
/ n7 f7 A& y5 W1 Z可以用關鍵字匹配分類嗎,比如標題有scenery的都一次分類到scenery去/ q& |, a- X6 X# ^  A0 M9 g5 c% M4 g; O
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 10:00:03 | 顯示全部樓層
還在搞自動阿,太佩服了。
$ h4 N5 a3 q0 d8 H9 p( I: ]" M
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 08:58:33 | 顯示全部樓層
有魚不換酒 發表於 2013-7-10 02:41 / X4 U. |- _6 y
能詳細說下怎麼用嗎?
& ]$ S" }4 u. e. {: R  G. I9 J我是不是建一個php文件,把這個句子寫進去
+ ^2 ?3 Q. Q, T% [( L比如

7 L6 d# R3 |1 _- A0 g! E先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.
& G( F$ M8 T  p6 R; c+ ]- n) |8 e5 e* k! D% T
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-11 16:03:57 | 顯示全部樓層
shanguiyao 發表於 2013-7-11 08:58
1 ~4 a6 _# L4 k* |- _先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.
) x% N  D: i. k/ @* f- t! x9 k8 r, E" h
...
- R& g; H0 Q; R/ M6 r- N
每一個post的標題都是不一樣的呀.這樣不是要為每一個post文章調用一次你這個存儲過程嗎?7 y5 M. q* \5 y4 w' i8 S
批量調用一次,不所有包含某個關鍵字標題的文章都添加一個分類去.這樣如何實現呢
- G$ R7 p/ m5 p4 h- A6 ~/ d
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 20:20:41 | 顯示全部樓層
對,每一個post標題調用一次.可以批量生成的吧?寫個sql都拼出來要執行的一堆存儲過程語句了.應該不太難吧." m" ^# P$ k! l- }5 @9 J
回復 给力 爆菊

使用道具 舉報

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

本版積分規則

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

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

GMT+8, 2025-7-11 08:57

By DZ X3.5

小黑屋

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