0

Problem Summary:

I am trying to write a SQL query that returns only my custom taxonomy terms, but the table (wp_terms) contains all of the terms, even ones that I don't want (such as category terms, and nav_menu terms).


This is my present query:

function myajax_inputtitleSubmit_func() {
global $wpdb;
global $customTerms;

$acInput = $_GET['input'];
$mydb = new wpdb('','','','');//commented out on purpose
// check nonce
$nonce = $_GET['nextNonce'];    
if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) )
    die ( 'Busted!');


//$custTermQuery = "SELECT slug FROM wp_47_terms";
$custTermQuery = "SELECT slug FROM wp_47_terms WHERE slug LIKE '%" . $acInput . "%'";

$customTerms = $wpdb->get_results($custTermQuery); 

// generate the response
//$response = json_encode( $_GET );
$response2 = json_encode( $customTerms );
$acInput_encoded = json_encode( $acInput );


// response output
header( "Content-Type: application/json" );
//echo $response;
echo ($response2);
//echo ($acInput_encoded);

// IMPORTANT: don't forget to "exit"
exit;

}


This is my table wp_47_term_taxonomy:

This is my table wp_47_term_taxonomy:

  • As you can see, my query gets all slugs even things like footer1, utility-links, and uncategorized. I don't want that. I only want the slugs that are part of my custom taxonomy wcmc_keywords.

This is my table wp_47_terms:

wp_47_term_taxonomy

  • I believe this table can be used somehow in a query to only show slugs with the taxonomy wcmc_keywords. This table shows association between 'Taxonomy' and 'term_id'.

CONCLUSION:

I am trying to use WordPress tables and SQL queries to obtain only the slugs of my custom taxonomy. Unfortunately, I am new at SQL queries, and even though I feel like I am on the right track, I would please like some help understanding what changes I need to make to my query.

  • Why SQL? get_terms() will do this. – s_ha_dum May 22 '14 at 18:09
  • Hello, sorry I needed to update my posting because I actually put in the wrong query on accident. I am not particularly opposed to get_terms(), however as you should be able to notice from the updates, I am using the terms as part of jquery autocomplete. WPDB works well for this because I can do things like %" . $acInput . "%, which returns data according to matching user input in realtime. Perhaps that is possible with get_terms(), if so, I was unaware. Thanks! – Daniel Dropik May 22 '14 at 18:17
0

If you take a look at get_terms() documentation it accepts search argument, which will perform LIKE match against term name and slug.

  • Thanks that will probably be really helpful, I would like to try it out some time. At this point, I am still curious about how to achieve my goal with SQL, even though get_terms() could be much more elegant. I am grateful. – Daniel Dropik May 22 '14 at 18:48

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.