I moved your topic to the Hacks forum, it’s the best place for custom coding questions.
Consider changing your form’s method to “post”. Then you can conditionally only run the query/results part of your template when the request method is “POST”. If it’s “GET” just output the form. Also, the URL is cleaner because it doesn’t have data tacked on to the end.
if ('GET' == $_SERVER['REQUEST_METHOD']) \\do the form
else \\do the query
The form’s action attribute needs to be the page’s permalink.
action="<?php the_permalink(); ?>"
Assuming you use the POST method, you can get the selected values from $_POST[‘Types’] and $_POST[‘Year’]. If you want to stay with GET, then use $_GET instead.
Hi
Thank to help me for the first part.
Now I have:
<form method="post" id="searchform" action="<?php the_permalink(); ?>">
<?php if ('GET' == $_SERVER['REQUEST_METHOD']) {}
else {
echo $_POST[Types].'</br>';
}
?>
<h5>Sélectionnez un type de document:</h5>
<?php wp_nonce_field( plugin_basename( __FILE__ ), 'types_noncename' );
$type_IDs = wp_get_object_terms( $post->ID, 'Types', array('fields' => 'ids') );
if ('GET' == $_SERVER['REQUEST_METHOD']){
wp_dropdown_categories('taxonomy=Types&hide_empty=0&orderby=name&name=Types&show_option_none=Selectionnez un type&selected='.$type_IDs[0]); }
else{
wp_dropdown_categories('taxonomy=Types&hide_empty=0&orderby=name&name=Types&show_option_none=Selectionnez un type&selected='.$type_IDs[0]);
}?>
<span class="input-group-btn2">
<button class="btn2 btn-search2" type="submit">Rechercher <i class="fa fa-search"></i></button>
</span>
</form>
I’m trying to always disply my filter combbox and the result, to have the ability to adjust filter at all time.
How to memorized the selected value into the dropdown ?
I only get id but string.
I’m looking for 3 things
1) memorized selected items into the comboobox
2) display value in the page
3) passing it into the WP_query
You need quotes around field names in $_POST: $_POST['Types']
At this point they are associative array keys, so use the same syntax. Note that the contained value from WP dropdown fields is the taxonomy term ID. In general, the ID is easiest to use in code anyway, but it means little to us humans.
$_POST['Types'] is a PHP variable like any other, so you can use it, save it or output it like any other variable. To show the selected dropdown item as the one last selected, in the POST version (the second, “else” version) replace $type_IDs[0] with $_POST['Types']
You can output it (or display it, same thing) like any other variable:
echo "Types term ID: $_POST['Types']</br>\n";
You can use it in a WP_Query argument array like any other variable:
$query_args = array(
'post_type' => 'mycustompost',
'tax_query' => array(
array(
'taxonomy' => 'types',
'field' => 'term_id',
'terms' => $_POST['Types'],
),
),
);
If you want to reverse the logic of ‘GET’ == $_SERVER[‘REQUEST_METHOD’], you can use either of the following (then you don’t need {} else ):
'GET' != $_SERVER['REQUEST_METHOD']
'POST' == $_SERVER['REQUEST_METHOD']
Hi
Thank you
I have update wy dropdown code. It is fine now.
For:
echo "Types term ID: $_POST['Types']</br>\n";
I got the tag index not the value, but I use it only for development
For the query, I’m updating it.
When I pass
if($_POST['Types'] != -1)
{
echo "1";
// types
$tax_query = array(
'taxonomy' => 'Types',
'field' => 'term_id',
'terms' => $_POST['Types'],
);
}
$query_args = array(
'post_type' => 'annales_cg',
'tax_query' => $tax_query,
'posts_per_page' => 25,
'order' => 'DESC',
'paged' => $paged);
sometimes it is working sometimes not.
I have one question: in my form do I need
wp_nonce_field( plugin_basename( __FILE__ ), 'types_noncename' );
Or how can I use it efficently ?
-
This reply was modified 9 years, 4 months ago by
benoitf92.
The purpose of a nonce field is to ensure the POST request is coming from your form and not some hacker’s hijacked computer. This is essential when you are saving input to the DB or when the data needs to come from a trusted user. If the data is only used to form a query, it’s not absolutely required, but might still be desirable to help prevent someone from scraping data from your site or similar abuses. Of course you need to verify the nonce value before doing anything for this measure to be effective.
If you are getting the option index in $_POST[‘Types’], that would explain what’s not working. I was assuming it contained the taxonomy term ID. The “terms” query argument needs to be the term ID. If you are getting an index, it needs to be translated to a term ID. Ideally, the dropdown options should have the ID assigned as the value, which is then sent as the selected value. At least that is how it works on my site with wp_dropdown_categories().
Are you sure they are index numbers? The following is one option from my site’s category dropdown, called with all defaults, no arguments:
<option class="level-0" value="69">Important</option>
The category term ID is 69, the value sent with the form submit. For comparison, an easy way to determine the term ID of your categories is go to the taxonomy term edit page and hover your cursor over the “View” action link. Examine the URL for this link, it will have an URL parameter “tag_ID=” followed by the ID for that term.
Hi,
I’m woking on pasing parameters to the Query.
Yesterday, I got some results like:
<form method="post" id="searchform" action="<?php the_permalink(); ?>">
<?php $type_IDs = wp_get_object_terms( $post->ID, 'Types', array('fields' => 'ids') );
if ('GET' == $_SERVER['REQUEST_METHOD']){
wp_dropdown_categories('taxonomy=Types&hide_empty=0&orderby=name&name=Types&show_option_none=Selectionnez un type&selected='.$type_IDs[0]); }
else{
wp_dropdown_categories('taxonomy=Types&hide_empty=0&orderby=name&name=Types&show_option_none=Selectionnez un type&selected='.$_POST['Types']);
}?>
</br>
<span class="input-group-btn2">
<button class="btn2 btn-search2" type="submit">Rechercher <i class="fa fa-search"></i></button>
</span>
</form>
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
if ('POST' == $_SERVER['REQUEST_METHOD']) {
echo "Types : ".$_POST['Types']."</br>\n";
if($_POST['Types'] != -1)
{
$query_args = array(
'post_type' => 'mycustompost',
'tax_query' => array(
'taxonomy' => 'Types',
'field' => 'term_id',
'terms' => $_POST['Types'],),
'posts_per_page' => 25,
'order' => 'DESC',
'paged' => $paged);
}
else
{
$query_args = array(
'post_type' => 'mycustompost',
'posts_per_page' => 25,
'order' => 'DESC',
'paged' => $paged
);
}
}
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
But, now the Query is nether filetring by taxonomy term. I do not understand why.
EDIT: the echo is working fine and display me the id of the taxonomy term
-
This reply was modified 9 years, 4 months ago by
benoitf92.
Oh so close!
It took me a while to spot this one. Tax queries need to be an array of arrays. This allows for very complex tax queries. When there is only one tax query argument this requirement does not make a lot of sense, so it’s easy to forget to include the extra array. The outer array is always required no matter what.
'tax_query' => array( array(
'taxonomy' => 'Types',
'field' => 'term_id',
'terms' => $_POST['Types'],
),),
There is one other small issue. On the initial page load, the GET request, we get a PHP warning that $query_args is not defined on the new WP_Query line. The generic, unrestricted query is never assigned for GET requests, only for POST requests when a Type selection is not made. There’s all sorts of ways to correct this. One possibility is to combine the POST check and not -1 check into one conditional so the else condition would apply to GET requests.
if('POST' == $_SERVER['REQUEST_METHOD'] && $_POST['Types'] != -1 ) //do a tax query
Hi,
thank you for your help.
With array of arrays it is fine.
I include 3 taxonomies in my form, I use differents $_POST['Types'] != -1 to add or not them into the query.
Yes, that’s a good plan. You just need to accommodate the situation when there is no $_POST data at all, as with the initial GET request before any tax filters are submitted. If you define WP_DEBUG as true in wp-config.php, you’ll see when PHP is complaining about such things. Put it back to false after you’re done testing, debug errors exposed on a production site are not good.
I use
if('POST' == $_SERVER['REQUEST_METHOD'])
to detect if I have some value in other situation I do nothing
If yes I have some if else condition to check which filter I have. I pass to the WP_Query only valid condition. I delete wrong parameters.
Understood. All I’m saying is the code you last posted throws a PHP warning with a GET request, so there is a technical gap in your logic flow. If you don’t mind PHP warnings in your code, then that’s fine, everything works as it should with warnings in this case.
I imagine you’ve moved on from that code version by now, so maybe all is fine now either way. If you’re happy I’m happy 🙂
Hi
Yes I have a warning
I solve it by
wp_dropdown_categories('taxonomy=Types& hide_if_empty=0&orderby=name&name=Types&show_option_none=Selectionnez un type&selected=0');
No warning from GET or POST methods.
The last warning is
Notice: Constant EMPTY_TRASH_DAYS already defined in C:\wamp\www\mysite.com\wp-config.php on line 94
OK, good, as long as the code we’re working on isn’t throwing warnings.
The redefined constant doesn’t really hurt anything other than your error logs could fill up with the same warning over and over. I’d suggest you get rid of one definition or the other, but it’s not required for everything to work.