0

I am wondering if there is any reason why the following code would not work?

My situation: I have a custom category set up called 'Issue'. What I want to do is get the most newly created category (which is 'Volume 2 Issue 1') and get its ID so that I can run the plugin function z_taxonomy_image_url($currentID); which outputs a URL based on the category identified by $currentID

I have a very hacky solution at the moment, but if anyone can figure out why the last 3 lines of the following bit of code doesn't work, it would be fantastic.

Alternative and cleaner solutions would also be appreciated.

$taxonomy=wp_list_categories('taxonomy=issue&echo=0&number=1&orderby=ID&order=DESC&show_count=0&style=none&title_li=');
$tax = strip_tags($taxonomy);
$newtax = "'".trim($tax)."'";
$getID = get_term_by('name', $newtax, 'issue');
$currentID = $getID->term_id;

Doing a var_dump on $getID only results in bool(false)

The interesting thing is that this code works (but I cannot use it because it would require manually changing the code each time I create a new category).

$getID = get_term_by('name', 'Volume 2 Issue 1', 'issue');
$currentID = $getID->term_id;

Is there something with get_term_by that it rejects using $newtax but accepts 'Volume 2 Issue 1' ? If yes, is there a workaround or do I need to look for some other way of doing this?

I am not very well versed in PHP and WP codes. I am not afraid of tinkering, but my knowledge is only basics :( Please bear with me.

Thank you for your time.

  • Just a hint: Not using query string syntax, but an array instead will make it much easier to debug in the future. Also query string syntax is deprecated and will be remove in the future. – kaiser Oct 24 '12 at 20:10
1

You can use this function http://codex.wordpress.org/Function_Reference/get_categories

The parameters passed are basically the same except it returns an object containing all the category information. Using this function can replace the first 4 lines of your code. Now your final code becomes:

$getID = get_categories('taxonomy=issue&number=1&orderby=ID&order=DESC');
$currentID = $getID->term_id;
  • Gave it a shot but it wouldn't display the URL so I did a var_dump on $currentID and it output NULL – Arkuen Oct 25 '12 at 2:40
  • are you sure there is a term in the database matching the get_categories criteria? Acc. to the codex at the provided link, it should be working – Mridul Aggarwal Oct 25 '12 at 8:47
  • Yup. Under the 'Issue' taxonomy are: Volume 1 Issue 1, Volume 1 Issue 2, Volume 1 Issue 3, and Volume 2 Issue 1. I posted an answer below how removing the apostrophes from the third line solved the problem. – Arkuen Oct 25 '12 at 10:32
0

Removed the single quotes I had been adding to $newtax got it to work (as following:)

$taxonomy=wp_list_categories('taxonomy=issue&echo=0&number=1&orderby=ID&order=DESC&show_count=0&style=none&title_li=');
$tax = strip_tags($taxonomy);
$newtax = trim($tax);
$getID = get_term_by('name', $newtax, 'issue');
$currentID = $getID->term_id;

For this, the matter is presently solved.

However, if anyone can propose a better/cleaner solution, that would be excellent.

Thank you!

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.