0

I have an Ajax call to a WooCommerce shortcode as follows:

global $wpdb;
// get_the_data
$term_id = $_POST['term_id'];
do_shortcode [products category='$term_id'];

which works OK, but I need to be able to determine which category I am in the loop-start.php, which I have overwritten to display things differently depending on the category.

I have tried numerous ways to get the category within the loop-start.php

global $wpdb;
global $wp_query;

$tableTitle = $wpdb->get_results(
    "SELECT slug FROM `wp_terms` WHERE name = '$page_title'"
);
$page_title = $tableTitle[0]->slug;
echo "<h2>page_title is ".$page_title."</h2>";

$cat = $wp_query->get_queried_object();
echo "CAT IS:".print_r($cat,true); // the category needed.

echo "<h2>query_var = ". get_query_var('cat') ."</h2>";

Every way that I have tried returns nothing. How can I get the category or some other indicator of what category this shortcode is being built with?

Rich
  • 37
  • 6
  • You should be able to use [`get_queried_object()->term_id;`](https://stackoverflow.com/a/12289287/6049581) or alternatively just [`get_queried_object_id();`](https://stackoverflow.com/a/42928542/6049581) – Frits Jul 24 '19 at 12:49

1 Answers1

0

I think I figured it out. I can use $_POST['term_id']

$tableTitle = $wpdb->get_results(
    "SELECT slug FROM `wp_terms` WHERE term_id = '$term_id'");
$page_title = $tableTitle[0]->slug;
echo "<h2>page_title is ".$page_title."</h2>";
Rich
  • 37
  • 6