1

The goal is that when you are on our /upcoming-events page you can then filter by type of event or region.

After you select the option from the dropdown, I want to echo in that page title into the breadcrumb. For example if you wanted to see events near you in Palo alto, you would select Northern California. Here is the code for that selection

<a class="dropdown-item" href="<?= $full_uri . "?tag=northern-california" ?>">
    Northern California
</a>

If you found no events near you, you could select the entire state.

<a class="dropdown-item" href="<?= $full_uri . "?tag=california" ?>">
    California
</a>

Or if you were available to travel anywhere, you could select by event (in an additional filter)

<a class="dropdown-item" href="<?= $full_uri . "?tag=seminar" ?>">
    Seminar
</a>

I was hoping that it would be solved with is_tag since the url ends with that tag selected, because that is how the filter is set up to direct you, but it won't work(displays nothing). Only has_tag works, but the problem is that then every string gets echoed for each tag, in this case the region, state, and event type. Here is my php to echo the string

if(has_tag( 'northern-california' || 'southern-california' || 'nevada' || 'webinar' || 'seminar' || 'industry-events')) {
    echo '&nbsp;/ ' . '<a href="<?php echo $event_tag; ?>"><span class="event-bread">';
}

if(is_tag( 'northern-california' )) {
    $event_tag = 'northern-california';
    echo "Northern California";
}

if(is_tag( 'northern-california' )) {
    $event_tag = 'northern-california';
    echo "Northern California";
}
/* other regions omitted for brevity */

if(is_tag( 'seminar' )) {
    $event_tag = 'seminar';
    echo "Seminars";
}
/* other event types omitted for brevity */

/* i think this below is a sloppy way to close the div */
if(has_tag( 'northern-california' || 'southern-california' || 'nevada' || 'webinar' || 'seminar' || 'industry-events')) {
    echo '</span></a>';
}

Update 1

Based on an answer I added this in my functions.php

function event_tag_url() {
    add_filter( 'query_vars', function( $eventVars ) {
        $eventVars[] = 'eventTag';
        return $$eventVars;
    }, 10, 1 );
}

Then in my category.php I changed to a switch statement

switch( get_query_var( 'eventTag' ) ) {
    case 'california':
        $eventTag = 'california';
        echo "California";
    break;
    case 'northern-california':
        $eventTag = 'northern-california';
        echo "Northern California";
    break;
    case 'southern-california':
        $eventTag = 'southern-california';
        echo "Southern California";
    break;
    case 'seminar':
        $eventTag = 'seminar';
        echo "Seminars";
    break;                                
    default:
        $eventTag = 'all';
    break;
}

This is not echoing anything though. and still does not seem like it would solve the problem of what happens when you have multiple tags. I think the solution should look for what tag the page is set to?

  • There's no reason to have your add_filter() function wrapped inside of another function. Your filter will not be added unless you call that function before WP tries to filter query_vars. Also, you're returning $$eventVars. There's an extra $ which will return nothing since ${$eventVars} doesn't exist. – Nathan Johnson Mar 30 '17 at 19:10
1

is_tag() returns whether the query is for an existing tag archive page. This isn't what you want to use.

Instead, use get_query_var().

get_query_var( 'tag' ) will look at the tag query variable in the URL. For instance, if your URL is

https://example.com/?tag=this&other=that

then using:

  • get_query_var( 'tag' ) will return this
  • get_query_var( 'other' ) with return that

Instead of using a lot of ifs, we can use the PHP switch construct.

//* Use switch instead of a long series of ifs
$tag = get_query_var( 'tag' );
switch( $tag ) {
  case 'northern-california':
    $tag = 'Northern California';
    break;
  case 'seminar':
    $tag = 'Seminar';
    break;
  //* Etc.
  default:
    $tag = 'All';
    break;
}
//* Do something useful with $tag

But to use get_query_var() for a custom query variable, we need to add the query var to the array of variable names that WordPress will retrieve using the function. This can go in your theme functions.php or in a simple plugin, as long as it's loaded before you try to get_query_var().

The string name used in the get_query_var() function must be exactly the same as the name used in the filter. If you want to use eventTag as your query var, then you need to add that to the query_vars array and use that as the query var in the URL:

https://example.com/?eventTag=california

This would go in your functions.php file

//* Allow custom query var 'tag'
add_filter( 'query_vars', function( $vars ) {
  //* Use whatever custom query var you want, but it needs to be exactly the same
  $vars[] = 'tag';
  $vars[] = 'eventTag';
  return $vars;
}, 10, 1 );

If you don't like using closures:

add_filter( 'query_vars', 'wpse_261930_query_vars', 10, 1 );
function wpse_261930_query_vars( $vars ) {
  $vars[] = 'tag';
  return $vars;
}

It's unclear how you would structure your URL for two different tags.

  • I imagine $vars is not meant to be used literally? perhaps another name might be useful to make it more obvious like $eventVar – JGallardo Mar 30 '17 at 18:28
  • $vars is just the name of the array passed to the function. It can be named whatever makes sense in your application. – Nathan Johnson Mar 30 '17 at 18:34
  • Because we already had the generic $tag in the project, i made some changes before I tried it, please see revised question. I also just went through the code and modified per the project, but I seem to be missing something. also not sure how this addresses the concern about what happens when you have multiple tags. though i guess it's ok since the case is looking for which tag is currently selected. – JGallardo Mar 30 '17 at 18:55
  • 1
    Ok with a couple of mods I got it to work. Will post my own version of the answer, but accepting yours as the answer. – JGallardo Mar 31 '17 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.