title: Retrieve Taxonomy Term ID from Term Name?
body: <p>I am using the following code to do a query on my database from within my page template by using the <code>term_taxonomy_id</code> which I retrieve from the end of the URL. </p>
  
  <p>My URL looks something like this when using this template <code>http://mysite.com/county/18</code> with 18 in this case being the <code>term_taxonomy_id</code></p>
  
  <p>My code is as follows:</p>
  
  <pre><code> &lt;?php
   /*
   Template Name: County
   */
   $ex = explode("/",$_SERVER['REQUEST_URI']);
   //print_r($ex);
  
  get_header(); 
  
  // Global query variable
   global $woo_options; 
   global $wp_query;
   global $wpdb;
   $all_terms = $wpdb-&gt;get_results("SELECT *  FROM wp_term_taxonomy,wp_terms WHERE wp_term_taxonomy.parent='{$ex[2]}' AND wp_term_taxonomy.term_id=wp_terms.term_id ORDER BY wp_terms.name");
  
   //echo "SELECT name   FROM wp_terms WHERE term_id='{$ex[2]}'";
   $taxName = $wpdb-&gt;get_results("SELECT name   FROM wp_terms WHERE term_id='{$ex[2]}'");
   foreach ( $taxName as $tx ) {
  
   $nameTaxnaomy = $tx-&gt;name;
   }
  
   $getPostID = $wpdb-&gt;get_results("SELECT object_id   FROM wp_term_relationships WHERE term_taxonomy_id='{$ex[2]}'");
   foreach ( $getPostID as $tx1 ) {
  
    $post = $tx1-&gt;object_id;
   }
  
   ?&gt;
  </code></pre>
  
  <p><strong>Question:</strong>
  I however need to change my URL structure to use the term_name at the end of the URL and not the taxonomy_id as above. </p>
  
  <p>So now my URL looks like this <code>http://mysite.com/county/London</code></p>
  
  <p>When I do this, the database query does not work as it is exploding the URL to get the taxonomy_id at the end (18 in my example above) and then query the DB using that string. </p>
  
  <p><strong>Actual Question:</strong> Is there a way to get the <code>term_taxonomy_id</code> from the term name so I can use it in my db query?</p>
  
  <p>I am thinking something like </p>
  
  <pre><code>//explode the url to get the term name
  $dx = explode("/",$_SERVER['REQUEST_URI']);
  
  //now get the id from the term name
  $ex = //get the term_taxonomy_id from $dx 
  </code></pre>
  
  <p>Any help appreciated</p>
  
  <p>Cheers</p>
  
  <p><strong>Update</strong> - With some tips below I have made the string look like this. My taxonomy name is called <code>location</code></p>
  
  <pre><code>//define $ex byt ge_term_by
  $ex = get_term_by('slug',explode("/",$_SERVER['REQUEST_URI']),'location');
  </code></pre>
  
  <p>When I echo <code>$ex</code> I don't get anything so something wrong still :(</p>