Community Forums › Forums › Archived Forums › Design Tips and Tricks › Using GET variables in custom query?
- This topic has 5 replies, 2 voices, and was last updated 6 years, 12 months ago by Victor Font.
-
AuthorPosts
-
April 21, 2017 at 2:03 am #205106trashParticipant
Hello,
I have an archive page for my custom post type. On top of the archive page I display three dropdown fields with custom taxonomies, they look like this:<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/location' ) ); ?>;" method="get">; <?php wp_dropdown_categories( 'name=city_of_location&taxonomy=city_of_location' ); ?>; <?php wp_dropdown_categories( 'name=size_of_location&taxonomy=size_of_location'); ?>; <?php wp_dropdown_categories( 'name=locations&taxonomy=locations' ); ?>; <input type="submit" name="submit" value="view" />; </form>
When I submit this form, I get an URL like this:
location?locations=1579&size_of_location=1581&city_of_location=1582&submit=view
In the archive template, inside the
genesis_loop
hook I do use:if ($_GET['size_of_location']) {
but I always receive an 404 error and have no access to the GET variable.
Does anybody how to solve this?
Thanks!
April 21, 2017 at 6:01 am #205112Victor FontModeratorThere's not enough information here to help with this issue. I wouldn't place the $_GET inside a loop. I would assign it to a variable and pass it into the loop function. $_GET should be available if you are reading them from a page called location.
As a suggestion, I would install the Kint debugger plugin. It make quick work of seeing the values on you are passing to the location archive page. All you do with Kint is add d($_GET); to the top of the page code and you'll see what is being passed to the page. https://wordpress.org/plugins/kint-php-debugger/
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?April 21, 2017 at 6:23 am #205118trashParticipantHey Victor,
so I have a custom post type with several custom post taxonomies:The CPT is registered with:
'has_archive' => 'location',
'rewrite' => array('slug' => 'location','with_front' => false),The Taxonomy is created with:
register_taxonomy( 'locations', 'location', array( 'hierarchical' => true, 'labels' => $labels, 'query_var' => true, 'show_admin_column' => true, 'rewrite' => array( 'slug' => 'location', // This controls the base slug that will display before each term ), ) );
Now I want to create a filter on the CPT's archive page with the contents of the taxonomy. That works, but when I select the option in the drop down and click on the submit button, I get 404 error.
I do not understand what you mean with "I would assign it to a variable and pass it into the loop function. $_GET should be available if you are reading them from a page called location."
When I visit /location?test=123 And place var_dump($_GET['test']); in my loop on archive-location.php I do not see the dump. So my question is basically how I can create a filter with a taxonomy on a custom post type archive page...? I need to access the loop to filter the available entries. My assumption was, that the 404 is thrown because the slug and the rewrite might interfere?
April 21, 2017 at 7:10 am #205123Victor FontModeratorIs the archive page named location? What is the full URL of the archive page?
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?April 21, 2017 at 7:11 am #205124trashParticipantYep /location shows all posts that are made with CPT "location"
April 25, 2017 at 11:06 am #205286Victor FontModeratorWithout examining your code, I can't even guess what's wrong. But I said earlier, I would not use a _get variable in a loop. You'll be reading the variable for every single post on a page.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet? -
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.