2016-08-12 92 views
1

.Hi傢伙自定義搜索WordPress的形式

如何執行以下代碼:

If ID search form == book 
    {The Post excluded from the search} 
else 
    {excluded a custom post type "book"} 

我的搜索形式是: 第一

<form id="book" > it's a custom post type 

第二

<form id="post" > //It's default Post type WordPress 

我可以這樣做嗎?

回答

0

將此代碼嵌入到您的function.php文件中。

它將搜索從後兩種類型的書的內容和發佈

function filter_search($query) { 
if ($query->is_search) { 
$query->set('post_type', array('post', 'book')); 
}; 
return $query; 
}; 
add_filter('pre_get_posts', 'filter_search'); 
+0

非常感謝您的回覆。 但不幸的是它沒有成功。看來我還沒有解釋過這個想法 我有兩種形式的搜索: 第一:我只想找書,當然有一個特別的主題。 另一種是在沒有書的情況下查看所有網站。 如下圖所示。 –

+0