Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:05:20+00:00 2026-06-14T14:05:20+00:00

I am using the following code to do a query on my database from

  • 0

I am using the following code to do a query on my database from within my page template by using the term_taxonomy_id which I retrieve from the end of the URL.

My URL looks something like this when using this template http://mysite.com/county/18 with 18 in this case being the term_taxonomy_id

My code is as follows:

 <?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->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->get_results("SELECT name   FROM wp_terms WHERE term_id='{$ex[2]}'");
 foreach ( $taxName as $tx ) {

 $nameTaxnaomy = $tx->name;
 }

 $getPostID = $wpdb->get_results("SELECT object_id   FROM wp_term_relationships WHERE term_taxonomy_id='{$ex[2]}'");
 foreach ( $getPostID as $tx1 ) {

  $post = $tx1->object_id;
 }

 ?>

Question:
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.

So now my URL looks like this http://mysite.com/county/London

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.

Actual Question: Is there a way to get the term_taxonomy_id from the term name so I can use it in my db query?

I am thinking something like

//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 

Any help appreciated

Cheers

Update – With some tips below I have made the string look like this. My taxonomy name is called location

//define $ex byt ge_term_by
$ex = get_term_by('slug',explode("/",$_SERVER['REQUEST_URI']),'location');

When I echo $ex I don’t get anything so something wrong still 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:05:21+00:00Added an answer on June 14, 2026 at 2:05 pm

    I think what you need is to use the get_term_by function from WordPress. You can see the full documentation of it here http://codex.wordpress.org/Function_Reference/get_term_by .

    The first 3 values are required, so you would need something like
    get_term_by(‘slug’,explode(“/”,$_SERVER[‘REQUEST_URI’]),’your_term_category_here’);

    Hope that helps get you started!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was using the following code to retrieve HTML snippets from a database table
I've got the following code that I'm using to retrieve records from the database
I'm using the following code to query a database from my jsp, but I'd
I'm pulling a joined query from the DB using the following code: $query =
I am trying to code the following query using Zend_Db api's: select * from
I am using following code to get bitmap from url. This function is used
I am using the following code to get data from the database( from cs
I have the following code to query a database using an ADO COMObject in
I'm using the following code to query my database: private const int PAGE_SIZE =
I'm using the following code to (attempt to) query a database: Query query =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.