0

WooCommerce has a built in product_tag url filter as such:

https://my-url.com/my-page/?product_tag=my-slug

Bringing back all the product_tags with the given slug in the url.

I am trying to do something relatively simple, retrieve the name of the product_tag and put it in a text heading block so the visitor knows what it is filtering.

The only thing I was able to figure out is in my woocommerce template page woocommerce-page.php is latching onto the global variable $wp_query

And then putting the product tags slug in a variable.

$producttag = $wp_query->query['product_tag'];

Questions:

Is this an appropriate way to retrieve the product tag slug? How do I convert the product slug into its actual Name?

Or is there a way to bypass the slug altogether and just get the product tags name? The only way I found how to get these variables was by looking at $wp_query, and that only returns the slug.

1

Not sure if I'm understanding you correctly, but why don't you just use $_GET['product_tag'] at the moment where you want to retrieve it?

UPDATE

If the product posts exist in the basic wp_{custom}_posts database or similar, this solution of a basic WP post query method may be more convenient to you:

$args = array(
  'post_type' => 'product_post_type',
  'name' => 'your_post_slug',
);

$arr_posts = new WP_Query($args);

if ($arr_posts->have_posts()) {
  while ($arr_posts->have_posts()) {
    echo $arr_posts->the_post();
      the_title();
      }
    } else {
    echo "<p class='error'>No posts with this slug found!<p>";
    }

This example queries in the database mentioned above for posts of the specified type and slug name and echoes out their title. You can also further specify query paramaters, as post tag names, category names, and so on, in the $args array. Also, the post type is not a must, just thought it's probably also unique to product posts if they're stored in the same database.

Just found this https://wp-staging.com/in-which-database-table-is-woocommerce-storing-products/, so the code above should definitely work.

| improve this answer | |
  • That's the first thing I tried, assumed WordPress didn't work this way. But on my page there is nothing in $_GET... it just returns an empty array(0) { } – bbruman May 2 at 10:45
  • Is the query parameter displayed in the URL?? Then it should work..? Otherwise, try via $_POST? and if that doesn't work, try using the $_REQUEST superglobal? – Joe May 2 at 10:48
  • Another thing I just noticed, there's a trailing slash in your URL, in front of your query '?' parameter, which is unusual..? – Joe May 2 at 10:50
  • I thought so too but reading other examples of how to url filter this was the example. I think /? is basically /index.php?.. it works to filter the products by tag either way and this doesn't change the fact that $_GET is empty – bbruman May 2 at 10:55
  • Ok, yeah well usually it is truly not possible to retrieve query parameters predefined by wordpress via $_GET, I had that problem when using month and year parameters when doing a calendar too. So if u use the plugin, retrieve the slug as u did. Concerning the name, where are the products stored in the database? To get the name, I imagine that yould query the records in the table of products by the slug (given that that is unique) and then search for the title column and done. Or, if products are stored as posts, make a normal wp post query to get the name in function of the slug. U see? – Joe May 2 at 10:59
0

I suppose this works. Still not sure if it is the best method. If someone knows of a better way to do this please chime in, I'll gladly adjust the answer.

function slugToName($slug) {
    global $wpdb;
    $query = 'SELECT name FROM `wp_terms` WHERE slug = "' . $slug . '"';
    $getname = $wpdb->get_results($query);
    $name = $getname[0]->name;
    return $name;
}
| improve this answer | |

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.