• kiannachauntis

    (@kiannarexia)


    Hey all!

    So, I’m coding my first search filtering system from scratch. I was originally using the Search & Filter plugin, which works like a dream, however with the way in which my client want to style the dropdowns and wire the search differently on certain pages, we’ve decided to nix the usage of plugins and opt in for coding something with just the bare minimum of what we need to get this working.

    That being said, I have a custom post type called “brands” and am currently working to add a handful of custom taxonomies via dropdowns so users may filter their search with ease. I’m really close to getting the code to work, however for whatever reason my current code seems to be grabbing the taxonomy term ID rather than its actual name and applying it to the search.

    So, if I am searching by brand location- and I select “California” from the location dropdown, then type clothing- the search url *should* render as

    https://www.mywebsite.com/?brand-location=california&s=clothing&post_type=brands

    instead- what I’m getting is

    https://www.mysite.com/?brand-location=48&s=clothing&post_type=brands

    Which obviously displays a “Nothing Found” page as well as a drop of table-flipping rage for myself :p

    I’m currently using this code within the search form to display my state location dropdown:

    <?php
    $args = array('taxonomy' => 'brand-location',
        'hide_empty' => 1,
        'echo' => 1,
        'name' => 'brand-location'
    );
    if (isset($_GET['brand-location'])){ $args['selected'] = intval($_GET['brand-location']);
    }
    wp_dropdown_categories( $args );?>

    I’m moderately new to PHP, and I’m sure the solution is fairly simple and will make me feel like an uber-n00b for not figuring it out sooner, but I’ve done quite a few searches at this point and can’t seem to find an answer that isn’t someone recommending the use of a plugin instead.

    Any ideas or help would be appreciated!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz

    (@bcworkz)

    “table-flipping rage” – lol, sorry to laugh at your misery, misery loves company. Taxonomy queries make me crazy. 3 joins for one silly term!

    There’s any number of ways to resolve this. I think I would simply alter the search query to use term_id instead of slug. For the sake of discussion, here’s a few more approaches:

    You could output your own select/option structure instead of using wp_dropdown_categories() where the option’s value attribute is set to the term’s slug instead of ID.

    OR – When you get the value from $_GET, use the ID to retrieve the associated term slug.

    There’s more, but they are beginning to get a bit silly, so I’ll stop.

    Thread Starter kiannachauntis

    (@kiannarexia)

    I’m afraid I’m not too sure on how to go about doing any of that lol :p I just recently started going deeper into the wordpress backend so I’m pretty new to this side of things.

    I definitely want to use the slug in the query rather than the id, what would the code look like to use the id to retrieve the slug so that can be plugged into the search url instead?

    Moderator bcworkz

    (@bcworkz)

    I think you misconstrued the sequence of events. While it’s possible to query the server to return the selected term slug so it can appear in the URL, the code involved is more complex and it requires an extra server request which is entirely avoidable.

    Since it’s important for the slug to appear in the URL, your best option is to drop the wp_dropdown_categories() and generate your own select/option form element. The basic code is fairly simple. It will get more complex if you have child categories or other quirks that need to be addressed. This example leaves out some details like class attributes, but it’s a basic structure from which you can build from.

    <select name="brand-location">
    <?php
       $cats = get_categories();
       foreach ( $cats as $cat ) {
          echo "  <option value=\"$cat->slug\">$cat->name</option>\n";
       }
    ?>
    </select>

    Thread Starter kiannachauntis

    (@kiannarexia)

    YES! This works like a charm thank you so much! Sorry for the misunderstanding, like I said I’m pretty new to this side of things. Dropped some cash on some new programming books and PHP today though so hopefully my understanding will evolve as I move forward from here ^__^

    Final code ended up looking like this:

    <select name="brand-location">
    <?php
    $args = array(
    	'taxonomy' => 'brand-location');
       $cats = get_categories($args);
       foreach ( $cats as $cat ) {
          echo "  <option value=\"$cat->slug\">$cat->name</option>\n";
       }
    ?>
    </select>

    This would probably be a question for an entirely different post (and one I think I’ve already started) but via this method would it be easier to “chain” three different select dropdowns together, the first one featuring top level parent taxonomies, the second showing children of whichever was selected from the first select, and then the third showing children of the second?

    Ideally this would be for a location filter, levels being Region->State->City

    Either way- THANK YOU!!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! Confusion is pretty much SOP in these forums, don’t worry about it.

    Inter-related dropdowns? Oooo! You’re in for some fun ?? (I’m unsure of the correct term for this either, I just like multi-syllabic words) Do any of those books include javascript or jQuery? If not, don’t go running back to the bookstore or amazon just yet, that aspect is largely copy paste programming. In addition, how those are implemented in WP is slightly different than what most references would demonstrate.

    You can actually avoid javascript/jQuery altogether if need be, but it makes for a poor user experience. After the first dropdown selection, send a new page request to the server. It responds with a new page containing the second dropdown contents based on the first selection. Repeat until all the parameters are collected.

    If all the possible variants of dropdowns is not too extensive, they could all be encoded into a javascript array. After each selection, javascript can look up the proper contents for the next dropdown and update just that one form element, no server requests involved. This obviously has it’s limits, you do not want your data array growing too large, it slows down page loads.

    The best solution and the best user experience involves AJAX, which I consider an intermediate coding skill. Still, if you’re patient and focus on small simple elements, one at a time, building on previous success, you’ll get there. AJAX implementation in WP also has it’s unique quirks, so generic references need to be taken with a grain of salt. A good place to start with this is https://developer.www.ads-software.com/plugins/javascript/ . The landing page is a little misleading, the topic is not just javascript. The topic actually continues for the next 4 pages, they are not separate topics like most of the manual.

    You’re likely now feeling overwhelmed, that’s natural. Don’t be discouraged though. Like I said, start simple and divide the project into small discreet parts, focusing on one part at a time. You’ll be surprised in the end on how little code is actually needed to accomplish the basics. Code grows large due to endless variations and making it user friendly. The part that does the bulk of the work is usually remarkably compact.

    The most important part is have fun! If you don’t get reward in seeing your code execute properly, you’re going to have a tough time with coding.

    Thread Starter kiannachauntis

    (@kiannarexia)

    I actually already have a good stack of books on Javascript and jQuery and I bought some books on Ajax yesterday as well along with that PHP book ?? It’s kind of my “new years resolution” (or whatever) to further my understanding of the programming side of things anyways, since as of late I have found my self doing less designing and more coding. I definitely don’t feel too overwhelmed, if anything I’m determined to learn! And I definitely get instant gratification when something I code actually works, even if it took me a while to figure it out. I love this stuff haha.

    Thank you so much for your feedback!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! Sounds like you’re all set, and have the right attitude. You’ll do well ??

    Thread Starter kiannachauntis

    (@kiannarexia)

    bcworkz- you have been really helpful thus far when I have posted anything regarding this current project and I thank you so much for that!

    It looks like my client is wanting to move forward on this a lot faster than I am able to learn the proper languages to accomplish what we need to. I apologize if this is out of line with forum rules- but do you by chance take on projects like this on a freelance basis and if so would you be interested in helping us to develop the final stretch of this search engine? We are of course willing to compensate you (duh) for your time. Please let me know if you are interested!

    (and I apologize if trying to hire someone in the forums is out of line with the rules :p we are desperate!)

    Moderator bcworkz

    (@bcworkz)

    Wow! I appreciate your confidence in me. Unfortunately, the mods here really aren’t kidding about the no payment rule. Asking or offering payment can result in your topic being closed.

    Thus I must respect this and decline, though I really appreciate the offer. You should be able to find help at least as qualified as I am at https://jobs.wordpress.net

    Best of luck on your project!

    Thread Starter kiannachauntis

    (@kiannarexia)

    Ah! ok, I wasn’t sure if that was a rule for sure or not- My apologies mods! Definitely disregard that then, thanks anyways though!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Grab Taxonomy name rather than ID in custom search’ is closed to new replies.