过期域名预定抢注

 找回密碼
 免费注册

wordpress批量更新category的方法

  [複製鏈接]
發表於 2013-7-9 10:58:26 | 顯示全部樓層 |閱讀模式
wordpress在線寫英文有些慢,我是在本地寫了一個簡單的錄入系統.寫好後批量導入進去.這牽涉到一些category或者tag的導入.為了批量導入.寫了一個存儲過程.然後批量調用存儲過程,可以把category導入進去.分享一下.
* g8 N+ }  ~2 x; W* L; U
6 x+ ?8 e/ p& y4 Y# J
" F: w- e7 ?( c$ x) g! kCREATE  PROCEDURE `p_add_article_category`(in v_term VARCHAR(300),in v_title varchar(2000))
$ L8 [7 I: I  u1 e3 Q- h0 t3 t3 cBEGIN/ ?/ Z# b7 ?, P6 G9 a* ]4 s
set @wp_post_id=IFNULL((select id from wp_posts where post_title=v_title limit 0,1),0);) o1 N4 W" n, V4 ?- [, K) i9 H
if (@wp_post_id>0) then
/ J; o, ~% G- r   set @term_id=IFNULL((select term_id from wp_terms where name=v_term limit 0,1),0);
5 l  \2 F( o1 e, m& S& e* N% f1 y- }   if (@term_id=0) then
! x: c, Z3 X9 w4 b& f$ ~       insert into wp_terms(name,slug, term_group) values(v_term,v_term,0);6 M6 i( @# ?; A% E
       set @term_id=(select term_id from wp_terms where name=v_term limit 0,1);" i. ^$ u6 Y" {- t" M0 Q$ m
   end if;
! X, S: s! z+ Y4 @# K% Q7 |  f   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);
) [) ?1 i1 F$ m5 A# j1 @   if (@term_taxonomy_id =0) then1 a# h$ ]+ K, B
           insert into wp_term_taxonomy(term_id,taxonomy,description,parent,count) values(@term_id,'category','',0,0);0 r5 P& A/ u) e3 u/ @: r( ^
           set @term_taxonomy_id=(select term_taxonomy_id from wp_term_taxonomy where taxonomy='category' and term_id=@term_id limit 0,1);
; w* P  d7 K) Q8 O   end if;8 j+ f; N1 d3 q' T+ u& n1 e7 u  K
/ F0 `$ ], R5 f6 t8 A/ ~& I7 w0 }+ C
   if(not EXISTS(select 1 from wp_term_relationships where object_id=@wp_post_id and term_taxonomy_id=@term_taxonomy_id)) then( ]4 R# J( D" i* Y6 J
           insert into wp_term_relationships(object_id,term_taxonomy_id,term_order) values(@wp_post_id,@term_taxonomy_id,0);
1 m; E8 }1 c  |8 _) k' J6 K: U           update wp_term_taxonomy set count=count+1 where taxonomy='category' and term_id=@term_id;8 Q+ ?- o1 u1 g/ R
   end if;: Z6 @  K& {, {4 N
end if;
, g- z2 ]4 q) ]6 ]9 T- BEND
  d  d" M- ?) M3 O' ?
; t6 `6 K2 P- L0 t  w2 O0 Y2 X: M調用方法:0 L  R* R: t0 H4 W* x
" D1 [: O4 c* c% |  i7 P
call  p_add_article_category('scenery','this is the title about scenery');
, `' O8 ^/ s$ H( S( o5 W1 f) v, k! N2 V) u( }9 v
) ]$ s9 u; u+ M0 Q. B

評分

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

查看全部評分

發表於 2013-7-9 11:19:57 | 顯示全部樓層
火車必用,謝謝分享。好東西
5 i: u" ?3 G# I; [+ ~
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-9 21:11:13 | 顯示全部樓層
好東西,謝謝分享!愛瘋!- k7 q8 H# A! D
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 02:41:54 | 顯示全部樓層
能詳細說下怎麼用嗎?( y- P3 v. O+ G7 S& @/ `: m4 \
我是不是建一個php文件,把這個句子寫進去; ]% R& L" W9 P7 p5 k, t
比如
, o0 q; L/ O0 z2 G" zcall  p_add_article_category('scenery','this is the title about scenery');5 f8 O$ B$ e( O/ N' J5 p; s
call  p_add_article_category('scenery','this is the title about scenery 2');
' w; z1 Q  k2 k( P! @call  p_add_article_category('scenery','this is the title about scenery 3');
: A; U" Y: G1 @5 O/ a/ G: p7 C! Z這樣是添加3個文章到目錄了.0 K$ C6 l4 h& |6 V( o/ k: t
如果一次改n條,是不是就要寫n條.
. y# i) Y/ t& A2 v$ ?- D! N. X可以用關鍵字匹配分類嗎,比如標題有scenery的都一次分類到scenery去
* |- n( \7 G+ D+ X- o
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 10:00:03 | 顯示全部樓層
還在搞自動阿,太佩服了。
9 l2 z. e( L3 L( u8 f
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 08:58:33 | 顯示全部樓層
有魚不換酒 發表於 2013-7-10 02:41 2 D; U$ q: _( h* R6 {2 U
能詳細說下怎麼用嗎?
" }  P) B( A0 ^  a6 |5 F# M! Y我是不是建一個php文件,把這個句子寫進去
! [3 ]% H' }1 |比如
- n/ r" l0 \8 g& K5 e! ~+ |
先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.
9 u1 M! H7 A6 M( P6 G% V8 |
+ p/ B( ?% X) f/ _" d0 `8 A
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-11 16:03:57 | 顯示全部樓層
shanguiyao 發表於 2013-7-11 08:58
' |- v4 j- X# x2 j& l先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.
5 U& V5 `3 }& \) `* ~9 G
# @) R! Z- z- _& ?0 s9 d ...
+ A6 q$ D! X2 Q+ |) N8 e
每一個post的標題都是不一樣的呀.這樣不是要為每一個post文章調用一次你這個存儲過程嗎?
0 E" q5 w" _2 j8 x4 F: I' l批量調用一次,不所有包含某個關鍵字標題的文章都添加一個分類去.這樣如何實現呢
; m* D5 Q: T0 E5 i& Q# d3 Y
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 20:20:41 | 顯示全部樓層
對,每一個post標題調用一次.可以批量生成的吧?寫個sql都拼出來要執行的一堆存儲過程語句了.應該不太難吧.
3 f( [8 T9 A4 w6 a" j
回復 给力 爆菊

使用道具 舉報

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

本版積分規則

點基

GMT+8, 2026-6-26 14:24

By DZ X3.5

小黑屋

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