过期域名预定抢注

 找回密碼
 免费注册

wordpress批量更新category的方法

  [複製鏈接]
發表於 2013-7-9 10:58:26 | 顯示全部樓層 |閱讀模式
wordpress在線寫英文有些慢,我是在本地寫了一個簡單的錄入系統.寫好後批量導入進去.這牽涉到一些category或者tag的導入.為了批量導入.寫了一個存儲過程.然後批量調用存儲過程,可以把category導入進去.分享一下.
+ f/ v/ {3 r4 F5 Q$ I; R
( u3 c/ a9 G! ?3 K( \1 B# Y
6 n- a3 X) R' o% `' _6 KCREATE  PROCEDURE `p_add_article_category`(in v_term VARCHAR(300),in v_title varchar(2000))# s9 e( D! P/ X0 i9 V
BEGIN6 N& O9 L# {; J; v) e
set @wp_post_id=IFNULL((select id from wp_posts where post_title=v_title limit 0,1),0);# b2 T5 C: V: t2 ]! u) D  {# K$ i+ U
if (@wp_post_id>0) then
4 I8 p0 A" z, u' H7 I   set @term_id=IFNULL((select term_id from wp_terms where name=v_term limit 0,1),0);$ S, @. n! s( }9 U0 `7 T
   if (@term_id=0) then4 ]# m1 b7 C; |; T
       insert into wp_terms(name,slug, term_group) values(v_term,v_term,0);, o# v5 k; f: E. e3 y
       set @term_id=(select term_id from wp_terms where name=v_term limit 0,1);$ D3 _2 H1 P+ e8 }
   end if;
# l6 a2 u( o/ Q   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' Z5 h7 |6 T" T5 E" s; F1 P- c   if (@term_taxonomy_id =0) then
+ q! Q; x; j1 N. H) ]; s" g' k6 B           insert into wp_term_taxonomy(term_id,taxonomy,description,parent,count) values(@term_id,'category','',0,0);
( d1 W. u/ {8 S7 R7 G/ f4 Z" {           set @term_taxonomy_id=(select term_taxonomy_id from wp_term_taxonomy where taxonomy='category' and term_id=@term_id limit 0,1);
: k- u/ v2 S. |1 K1 W   end if;
5 R& h, \5 X( I0 u" i) x4 V! G
% l; t; j  ?+ c   if(not EXISTS(select 1 from wp_term_relationships where object_id=@wp_post_id and term_taxonomy_id=@term_taxonomy_id)) then
3 o9 U% Y" p% a9 l           insert into wp_term_relationships(object_id,term_taxonomy_id,term_order) values(@wp_post_id,@term_taxonomy_id,0);
# p1 d/ v( T% e1 D) n9 e* P% P           update wp_term_taxonomy set count=count+1 where taxonomy='category' and term_id=@term_id;
8 q1 l6 G5 z3 R0 B+ l5 W   end if;
; \3 h  X6 W  G8 I8 T9 L% lend if;
# i4 i9 l6 W7 S3 Y- DEND
1 u1 V7 V' U/ r8 B# y  l: W9 I
- v* ]; |  t( Q. B6 G# M5 p  Z. r調用方法:
2 \+ ]& ^$ ^) t8 A8 l2 x) E3 k4 M# ]5 J% _9 h; u
call  p_add_article_category('scenery','this is the title about scenery');0 p: k' Y  ~, u2 _& h

. n) i- G6 n/ X- I* y3 O; |+ ^, g  e+ j, X

評分

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

查看全部評分

發表於 2013-7-9 11:19:57 | 顯示全部樓層
火車必用,謝謝分享。好東西% p  z; m( v- c5 B
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-9 21:11:13 | 顯示全部樓層
好東西,謝謝分享!愛瘋!
+ y7 N/ o9 x$ S* a# i* A
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 02:41:54 | 顯示全部樓層
能詳細說下怎麼用嗎?
! ?8 I8 v9 ^# z" U我是不是建一個php文件,把這個句子寫進去+ J1 `$ `  k& o: r/ O5 [( {2 G
比如
- M6 f. Z5 y4 h: k$ P& M  N# fcall  p_add_article_category('scenery','this is the title about scenery');; \' [, k' \0 T+ D1 @: _- V5 L
call  p_add_article_category('scenery','this is the title about scenery 2');7 X6 g3 b: a' Y  o8 }# r
call  p_add_article_category('scenery','this is the title about scenery 3');
" c* S- t; f9 `3 {這樣是添加3個文章到目錄了.
( s7 r. C: a4 I: a; C$ b如果一次改n條,是不是就要寫n條.
( O) u* j3 L. v可以用關鍵字匹配分類嗎,比如標題有scenery的都一次分類到scenery去8 P2 W8 m" k# M0 A. u
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-10 10:00:03 | 顯示全部樓層
還在搞自動阿,太佩服了。% y* j1 h! F2 O! D
回復 给力 爆菊

使用道具 舉報

 樓主| 發表於 2013-7-11 08:58:33 | 顯示全部樓層
有魚不換酒 發表於 2013-7-10 02:41   P: Y( J) l  ~8 S. m" G
能詳細說下怎麼用嗎?) m: A0 A  i+ S! j& R% G# [' d
我是不是建一個php文件,把這個句子寫進去
9 i/ g9 f% Z1 R' J+ c; o比如

& x: h' o4 V# N- B3 c/ F1 k6 m" n* n0 v先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.* u* w1 {  R% a0 _/ o; P4 H! ]
5 |7 p2 p- u+ ]1 F0 V! F
回復 给力 爆菊

使用道具 舉報

發表於 2013-7-11 16:03:57 | 顯示全部樓層
shanguiyao 發表於 2013-7-11 08:58
* `9 {* {  t& i2 i/ D先把存儲過程通過phpmyadmin註冊進去,然後批量調用就行了.一次可以弄幾千個.一下執行.
: Q, ~3 s. U& H8 Z( a
4 v9 M; Q0 m- p% ]4 L8 R! r ...

! h8 Y% C; L4 M! e8 y4 X8 `& c每一個post的標題都是不一樣的呀.這樣不是要為每一個post文章調用一次你這個存儲過程嗎?2 }" s3 g6 X( h# Z& E
批量調用一次,不所有包含某個關鍵字標題的文章都添加一個分類去.這樣如何實現呢
& d# n6 ^+ M9 Y" ]
回復 给力 爆菊

使用道具 舉報

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

使用道具 舉報

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

本版積分規則

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

點基

GMT+8, 2026-6-26 16:37

By DZ X3.5

小黑屋

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