过期域名预定抢注

 找回密碼
 免费注册

wordpress批量更新category的方法

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

. A6 V+ a$ V% m, ]
5 F" y4 S$ w8 N9 _% N3 Z: ECREATE  PROCEDURE `p_add_article_category`(in v_term VARCHAR(300),in v_title varchar(2000))
/ v, V9 L# f+ B# ^BEGIN
3 B0 I9 r2 l4 B! wset @wp_post_id=IFNULL((select id from wp_posts where post_title=v_title limit 0,1),0);
7 B0 L* a6 G# x6 @8 x' D% f; D% Dif (@wp_post_id>0) then$ u1 Y' B# `+ _0 Y
   set @term_id=IFNULL((select term_id from wp_terms where name=v_term limit 0,1),0);
5 Q! @) |3 I, P6 v& q   if (@term_id=0) then3 O7 D( s1 R# M  O, m, r, W
       insert into wp_terms(name,slug, term_group) values(v_term,v_term,0);
; x' d4 z; S+ [$ n! i       set @term_id=(select term_id from wp_terms where name=v_term limit 0,1);; n6 C6 L# Z/ I* p- Z& ~
   end if;' p! f& T9 [* N4 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);/ z) F) k4 x2 N0 F3 Y
   if (@term_taxonomy_id =0) then
) ]. w# ^* y) T9 h1 n# a  L# S           insert into wp_term_taxonomy(term_id,taxonomy,description,parent,count) values(@term_id,'category','',0,0);; W) q1 @: G* q: t& }. Q, ?8 C7 |7 \
           set @term_taxonomy_id=(select term_taxonomy_id from wp_term_taxonomy where taxonomy='category' and term_id=@term_id limit 0,1);
3 L% x( U8 U/ F7 G" p+ M   end if;! @- H% u; l0 p# V* ?7 m

7 n" \" X6 m; u8 n. @   if(not EXISTS(select 1 from wp_term_relationships where object_id=@wp_post_id and term_taxonomy_id=@term_taxonomy_id)) then
, l* i1 s0 U' M/ d* B# e/ D& s           insert into wp_term_relationships(object_id,term_taxonomy_id,term_order) values(@wp_post_id,@term_taxonomy_id,0);
- P! U7 {/ K0 g1 e: S# ]           update wp_term_taxonomy set count=count+1 where taxonomy='category' and term_id=@term_id;$ O% V& @# |& D1 P- ?
   end if;8 F3 N$ L& @4 @/ D0 n
end if;
+ [- W3 g, F! J% `+ n$ D  CEND
/ q0 E! I  E9 m6 t; Z2 i
9 E7 Y/ K* ^* R. O6 ^調用方法:
! t: C  q3 Z+ }, P( |2 x- Z; ?4 x, [( Z+ o; ~9 j9 O6 K
call  p_add_article_category('scenery','this is the title about scenery');
) u( m/ R; Z# z
+ Z, S$ O5 W/ J+ Q# X, c3 j# R) o5 x

評分

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

查看全部評分

發表於 2013-7-9 11:19:57 | 顯示全部樓層
火車必用,謝謝分享。好東西
! Z; o$ x9 l" X8 t$ K1 |
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-9 21:11:13 | 顯示全部樓層
好東西,謝謝分享!愛瘋!( S7 R% g" P) u7 Q/ m
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 02:41:54 | 顯示全部樓層
能詳細說下怎麼用嗎?
# k; M& n$ E7 o3 ~' f! f我是不是建一個php文件,把這個句子寫進去) V2 X3 a3 F& f/ O( b; L- T
比如  ?, y6 ]3 Z2 ^3 X/ W4 {
call  p_add_article_category('scenery','this is the title about scenery');
3 `; H8 P/ J* l  H% hcall  p_add_article_category('scenery','this is the title about scenery 2');6 F5 b3 B! d+ b) K
call  p_add_article_category('scenery','this is the title about scenery 3');3 ^5 F, @  g# R( f. f6 ~% s
這樣是添加3個文章到目錄了.( ]1 @/ w1 f+ K
如果一次改n條,是不是就要寫n條.' Q) X' n+ y' d4 |5 y
可以用關鍵字匹配分類嗎,比如標題有scenery的都一次分類到scenery去" q* d7 d: O; r& B2 m
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 10:00:03 | 顯示全部樓層
還在搞自動阿,太佩服了。
) ?% P5 _6 t- ]! t' m1 n: j% ~
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 08:58:33 | 顯示全部樓層
有魚不換酒 發表於 2013-7-10 02:41
5 A; _. P( J- ~能詳細說下怎麼用嗎?
2 l/ c1 V! J/ ~/ x/ q我是不是建一個php文件,把這個句子寫進去
( `* E" i3 \0 x1 n0 x比如
3 [' g7 E7 M" G. C& M' U: T
先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.' m7 _! d* f! U
' [+ g4 g% c5 S9 |; E2 e
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-11 16:03:57 | 顯示全部樓層
shanguiyao 發表於 2013-7-11 08:58
- M1 v/ X+ M! p9 |2 R* J2 P" i# F先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.8 o0 Z$ Z/ q9 n8 t( c: S: T

% n/ t& ?- @; q. g ...
( J% [. V& ^8 m% `0 s
每一個post的標題都是不一樣的呀.這樣不是要為每一個post文章調用一次你這個存儲過程嗎?
0 {& I9 P2 l# }3 w! F' v批量調用一次,不所有包含某個關鍵字標題的文章都添加一個分類去.這樣如何實現呢) S/ N% r$ g( A: _! }& a& c
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 20:20:41 | 顯示全部樓層
對,每一個post標題調用一次.可以批量生成的吧?寫個sql都拼出來要執行的一堆存儲過程語句了.應該不太難吧.8 c/ A3 w2 l0 @3 f3 x2 P9 i
回復 给力 爆菊

使用道具 舉報

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

本版積分規則

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

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

GMT+8, 2025-7-19 06:33

By DZ X3.5

小黑屋

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