在我的博客上,我成功地对文章标题和内容进行了研究,但在我尝试对文章和类别的自定义分类进行研究之后。使用meta_query()
和REGEXP
,无法正常工作。
我的表格:
<div class="zone_filters">
<form action="" method="GET">
<input type="hidden" name="page_id" value="249" />
<input type="text" name="search_input" />
<select name="type">
<option value="title">titre et texte</option>
<option value="category">catégorie</option>
</select>
<select name="orderby">
<option value="DESC">plus récent</option>
<option value="ASC">plus ancien</option>
</select>
<button type="submit">OK</button>
</form>
</div>
我的论点:
<?php
$orderby = 'DESC';
if($_GET['orderby']) {
$orderby = $_GET['orderby'];
}
$args = array(
'post_type' => 'article',
'posts_per_page' => 3,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
'orderby' => array('date' => $orderby)
);
if($_GET['type'] == 'title') {
$args['s'] = $_GET['search_input'];
}
elseif($_GET['type'] == 'category') {
$value = '^[a-zA-Z|\s]*(' . strtoupper( $_GET['search_input']) . '|' . strtolower( $_GET['search_input']) . ')[a-zA-Z|\s]*$';
$args['meta_query'] = array(array('key' => 'categories', 'value' => $value, 'compare' => 'REGEXP'));
}
$query = new WP_Query($args);?>
我是从the Codex学到的 而我无法找到我的代码和文档之间的差异。
答案 0 :(得分:2)
对类别使用税务查询。
$args['tax_query'] = array(array('taxonomy' => 'categories', 'field' => 'slug', 'terms' => $value));
根据你的HTML
<div class="zone_filters">
<form action="" method="GET">
<input type="hidden" name="page_id" value="249" />
<input type="text" name="search_input" />
<select name="type">
<option value="">titre et texte</option>
<option value="category-1">Category 1</option>
<option value="category-2">Category 2</option>
<option value="category-3">Category 3</option>
</select>
<select name="orderby">
<option value="DESC">plus récent</option>
<option value="ASC">plus ancien</option>
</select>
<button type="submit">OK</button>
</form>
</div>
<?php
$orderby = 'DESC';
if($_GET['orderby']) {
$orderby = $_GET['orderby'];
}
$args = array(
'post_type' => 'article',
'posts_per_page' => 3,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
'orderby' => array('date' => $orderby)
);
if($_GET['type'] == 'title') {
$args['s'] = $_GET['search_input'];
}
elseif($_GET['type'] == 'category') {
$value = '^[a-zA-Z|\s]*(' . strtoupper( $_GET['search_input']) . '|' . strtolower( $_GET['search_input']) . ')[a-zA-Z|\s]*$';
$args['tax_query'] = array(array('taxonomy' => 'categories', 'field' => 'slug', 'terms' => $value));
}
$query = new WP_Query($args);?>
答案 1 :(得分:0)
这是这样的:
表格:
<div class="zone_filters">
<form action="" method="GET">
<input type="hidden" name="page_id" value="249" />
<input type="text" name="search_input" />
<select name="categories">
<?php $allterms = get_terms('categories');
foreach($allterms as $term) : ?>
<option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
<?php endforeach ?>
</select>
<select name="orderby">
<option value="DESC">plus récent</option>
<option value="ASC">plus ancien</option>
</select>
<button type="submit">OK</button>
</form>
</div>
论点:
<?php
$orderby = 'DESC';
if($_GET['orderby']) {
$orderby = $_GET['orderby'];
}
$args = array(
'post_type' => 'article',
'posts_per_page' => 3,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
'orderby' => array('date' => $orderby)
);
if($_GET['search_input'] != '') {
$args['s'] = $_GET['search_input'];
}
elseif($_GET['categories']) {
$args['categories'] = $_GET['categories'];
}
$query = new WP_Query($args);?>