WordPress多重筛选并排序(按自定义字段、分类、标签等)

WordPress要实现文章多重筛选的核心函数就是wp_query( ),有php基础的,直接看官方文档应该就能解决标题所提到的WordPress多重筛选功能了。效果图实例:

下面我做一些简单的介绍:
假设文章有自定义字段:area 存放的是地区信息,比如重庆,比如北京等。另一个字段为type,存放的是比如,

<span class="token comment">// 分类</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'cat'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
 	<span class="token variable">$cat_name</span><span class="token operator">=</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'cat'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
<span class="token comment">//  标签</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'tag'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
 	<span class="token variable">$tag</span><span class="token operator">=</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'tag'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>

<span class="token comment">// 自定义字段:area</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'area'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
 	<span class="token variable">$array_temp</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'area'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'value'</span><span class="token operator">=</span><span class="token operator">></span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'area'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'compare'</span><span class="token operator">=</span><span class="token operator">></span><span class="token single-quoted-string string">'='</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 	<span class="token function">array_push</span><span class="token punctuation">(</span><span class="token variable">$metaArray</span><span class="token punctuation">,</span><span class="token variable">$array_temp</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
 
 <span class="token comment">// 自定义字段:type</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'type'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
 	<span class="token variable">$array_temp</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'type'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'value'</span><span class="token operator">=</span><span class="token operator">></span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'type'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'compare'</span><span class="token operator">=</span><span class="token operator">></span><span class="token single-quoted-string string">'='</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 	<span class="token function">array_push</span><span class="token punctuation">(</span><span class="token variable">$metaArray</span><span class="token punctuation">,</span><span class="token variable">$array_temp</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>

<span class="token comment">//正反顺序</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'order'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token variable">$order</span><span class="token operator">=</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'order'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span> <span class="token variable">$order</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'DESC'</span><span class="token punctuation">;</span>

<span class="token comment">//默认按时间排序</span>
<span class="token variable">$orderby</span><span class="token operator">=</span><span class="token single-quoted-string string">'date'</span><span class="token punctuation">;</span>

<span class="token comment">//如果用户设置了按文章特定字段价排序</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'orderby'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'orderby'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token single-quoted-string string">'views'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token variable">$array_temp</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'views'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 	<span class="token function">array_push</span><span class="token punctuation">(</span><span class="token variable">$metaArray</span><span class="token punctuation">,</span><span class="token variable">$array_temp</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 	<span class="token variable">$orderby</span><span class="token operator">=</span><span class="token single-quoted-string string">'meta_value_num'</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token variable">$orderby</span> <span class="token operator">=</span> <span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'orderby'</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//否则按wp自带排序字段</span>
<span class="token punctuation">}</span>

<span class="token comment">//获取上述参数,组成查询条件</span>
<span class="token variable">$args</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
	<span class="token single-quoted-string string">'meta_query'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$metaArray</span><span class="token punctuation">,</span>
         <span class="token single-quoted-string string">'category_name'</span> <span class="token operator">=</span> <span class="token operator">></span><span class="token variable">$cat_name</span><span class="token punctuation">,</span>
         <span class="token single-quoted-string string">'tag'</span><span class="token operator">=</span> <span class="token variable">$tag_name</span><span class="token punctuation">,</span>
	<span class="token single-quoted-string string">'order'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$order</span><span class="token punctuation">,</span><span class="token comment">//DESC,</span>
	<span class="token single-quoted-string string">'orderby'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$orderby</span><span class="token punctuation">,</span>
 <span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//查询文章</span>
 <span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WP_Query</span><span class="token punctuation">(</span> <span class="token variable">$args</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

输出:

<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">have_posts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token variable">$query</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">the_post</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span>

<span class="token tag"><span class="token punctuation"><</span>h1<span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post-name<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"><</span>a<span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">the_permalink</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">the_title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token punctuation"></</span>a<span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>h1<span class="token punctuation">></span></span>

<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">endwhile</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span>

我这里只设置输出了标题。大意就是如此。通过上面的代码,就可以实现,标签,分类,和area 和type四个条件的多重筛选。

<span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">'cat'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 这是指从网址中获取的值,比如 ?area=重庆&cat=food&tag=good&type=xxx&orderby=modified 则获取的是,重庆地区,分类为food,标签为good,类型为xxxx的文章,并按修改时间排序。

为了方便通过点击自动生成上述链接,你可能需要了解一下:add_query_arg()  和 remove_query_arg() 这两个函数。如果多重筛选需要分页,你可能需要 WP-PageNavi这个插件配合上面的代码使用

<span class="token function">wp_pagenavi</span><span class="token punctuation">(</span> <span class="token keyword">array</span><span class="token punctuation">(</span> <span class="token single-quoted-string string">'query'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$query</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

上述方法是WordPress实现多重筛选最优质的思路,网上其他有一些方法,比如通过多个tag等实现,但如果遇到需要自定义字段的区间筛选就歇菜了。

非常抱歉,你可能读完这篇文章还是云里雾里,实话说,如果你毫无编程基础,的确很难理解。我这里只是记录的一个实现思路。但一旦你熟悉就会豁然开朗,WordPress多重筛选真的很好很强大,这会拓宽你做网站的道路。

如果你实在有需求,可以联系我付费实现。

标签TAGS

与标签相关的文章

最新文章

热门文章

其他文章