admin管理员组文章数量:1122846
I am trying to create a Search Filter within WordPress to filter through all the custom taxonomies within a Custom Post Type.
I have created a function that generates the dropdown so I can output each option value as a slug for my filter which is working already. My only issue is that the "Select All" option is not working.
The results come back with nothing found when Selecting All. The filter can be here.
The code I have used to create the function is:
function adopt_custom_taxonomy_dropdown( $taxonomy, $orderby = 'date', $order = 'DESC', $limit = '-1', $name, $show_option_all = null, $show_option_none = null ) {
$args = array(
'orderby' => $orderby,
'order' => $order,
'number' => $limit,
);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
printf( '<select name="%s" class="postform">', esc_attr( $name ) );
if ( $show_option_all ) {
printf( '<option value="0">%s</option>', esc_html( $show_option_all ) );
}
if ( $show_option_none ) {
printf( '<option value="-1">%s</option>', esc_html( $show_option_none ) );
}
foreach ( $terms as $term ) {
printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
}
print( '</select>' );
}
}
and this is where the results are being pulled to...
<?php
if (isset($_GET["farm-type"]) && empty($_GET["location-farms"])){
$farm_type = $_GET["farm-type"];
$myquery1['tax_query'] = array(
array(
'taxonomy' => 'farm-type',
'terms' => array($farm_type),
'field' => 'slug',
),
);
query_posts($myquery1);
}
?>
<?php
if (isset($_GET["farm-type"]) && isset($_GET["location-farms"])){
$farm_type = $_GET["farm-type"];
$farm_location = $_GET["location-farms"];
$myquery2['tax_query'] = array(
array(
'taxonomy' => 'farm-type',
'terms' => array($farm_type),
'field' => 'slug',
),
array(
'taxonomy' => 'location-farms',
'terms' => array($farm_location),
'field' => 'slug',
),
);
query_posts($myquery2);
}
?>
I am trying to create a Search Filter within WordPress to filter through all the custom taxonomies within a Custom Post Type.
I have created a function that generates the dropdown so I can output each option value as a slug for my filter which is working already. My only issue is that the "Select All" option is not working.
The results come back with nothing found when Selecting All. The filter can be here.
The code I have used to create the function is:
function adopt_custom_taxonomy_dropdown( $taxonomy, $orderby = 'date', $order = 'DESC', $limit = '-1', $name, $show_option_all = null, $show_option_none = null ) {
$args = array(
'orderby' => $orderby,
'order' => $order,
'number' => $limit,
);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
printf( '<select name="%s" class="postform">', esc_attr( $name ) );
if ( $show_option_all ) {
printf( '<option value="0">%s</option>', esc_html( $show_option_all ) );
}
if ( $show_option_none ) {
printf( '<option value="-1">%s</option>', esc_html( $show_option_none ) );
}
foreach ( $terms as $term ) {
printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
}
print( '</select>' );
}
}
and this is where the results are being pulled to...
<?php
if (isset($_GET["farm-type"]) && empty($_GET["location-farms"])){
$farm_type = $_GET["farm-type"];
$myquery1['tax_query'] = array(
array(
'taxonomy' => 'farm-type',
'terms' => array($farm_type),
'field' => 'slug',
),
);
query_posts($myquery1);
}
?>
<?php
if (isset($_GET["farm-type"]) && isset($_GET["location-farms"])){
$farm_type = $_GET["farm-type"];
$farm_location = $_GET["location-farms"];
$myquery2['tax_query'] = array(
array(
'taxonomy' => 'farm-type',
'terms' => array($farm_type),
'field' => 'slug',
),
array(
'taxonomy' => 'location-farms',
'terms' => array($farm_location),
'field' => 'slug',
),
);
query_posts($myquery2);
}
?>
Share
Improve this question
edited May 26, 2014 at 12:56
damienoneill2001
asked May 24, 2014 at 17:06
damienoneill2001damienoneill2001
819 bronze badges
3
|
1 Answer
Reset to default 0Couple of notes:
- Your form
action
is incorrect:<form method="get" action="http://www.adoptapet.ie/archive-farm/">
. The page is<form method="get" action="http://www.adoptapet.ie/archive-farms/">
You should reorder your argument list for
adopt_custom_taxonomy_dropdown()
. As written, most of your arguments have default values and can be omitted when the function is called, except that as written argument five doesn't have a default so you can't in practice omit two, three, and four. Move$name
to the front-- much better usability.adopt_custom_taxonomy_dropdown( $taxonomy, $name, $orderby = 'date', $order = 'DESC', $limit = '-1', $show_option_all = null, $show_option_none = null )
- Don't use
query_posts()
, ever. It clobbers the main query and can cause numerous problems. - While I have not edited the code into the suggestions below, you
should not ever use user supplied data like
$_GET
without sanitizing it.$_GET
and$_POST
are not safe.
I think your problem is mostly a logic issue. This...
isset($_GET["farm-type"]) && isset($_GET["location-farms"])
.... is always going to be true. $_GET["farm-type"]
and $_GET["location-farms"]
are always going to be set when your form submits, even if they are empty. You should be using !empty()
instead.
And you can clean up this code a lot:
if (!empty($_GET["farm-type"])){
$farm_type = $_GET["farm-type"];
$myquery['tax_query'][] =
array(
'taxonomy' => 'farm-type',
'terms' => array($farm_type),
'field' => 'slug',
);
}
if (!empty($_GET["location-farms"])){
$farm_location = $_GET["location-farms"];
$myquery['tax_query'][] =
array(
'taxonomy' => 'location-farms',
'terms' => array($farm_location),
'field' => 'slug',
);
}
if (!empty($myquery)) {
$q = new WP_Query($myquery); // instead of wp_query()
var_dump($q);
}
And you probably want an AND
relationship between your two components.
if (!empty($myquery)) {
if (1 < count($myquery['tax_query'])) {
$myquery['tax_query']['relation'] = 'AND';
}
$q = new WP_Query($myquery);
var_dump($q->request); // debug
}
Now, if you pass an empty string for your "Select All" option instead of a number, I believe you should get the results you are looking for.
本文标签: phpSelect All not working in a WordPress search filter
版权声明:本文标题:php - Select All not working in a WordPress search filter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736294323a1929332.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
query_posts
, there is no reason this couldn't use aWP_Query
object or thepre_get_posts
filter to implement this – Tom J Nowell ♦ Commented Dec 24, 2021 at 0:06