• Hello,

    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.

    I have a very hacky solution at the moment, but if anyone can figure out why the last 3 lines don’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');
    $curID = $getID->term_id;

    Doing a var_dump on $getID only results in this: 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');
    $curID = $getID->term_id;

    (cross-post from https://www.ads-software.com/support/topic/get_term_by-and-variables?replies=1 because I didn’t know this subforum existed. Sorry mods!)

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Don’t add the single quotes to the value in $newtax. Right now you are basically try to say "'Volume 2 Issue 1'" == "Volume 2 Issue 1" which is wrong. The single quotes delimit the string, they are not part of the string. PHP knows the limits of the string in a variable without you adding extra quotes.

    Thread Starter Anna

    (@ohthenoes)

    Ah shoot, that worked perfectly. Thank you!

    Though apparently “query string syntax is deprecated and will be remove in the future” ? Does this mean I should try to look for a different way of going about things instead of this?

    Moderator bcworkz

    (@bcworkz)

    I think it’s referring to your argument being in string form instead of as an associative array. See Template Tags/wp list categories for an example of how to properly define an associative array query argument.

    And a suggestion for improvement, use get_categories(), then you will not need to strip tags or get terms. It returns an array of objects, so assuming the return value is still assigned to $taxonomy and only the first or only object contains the ID you want, you could refer directly to the ID as $taxonomy[0]->cat_ID

    Thread Starter Anna

    (@ohthenoes)

    Hm, I ended up with the following because it was easy to modify the number to be displayed (I have three different sections: the most recent, the most recent 3, and all). Would this be sufficient or can it be changed further?

    <?php foreach (get_terms('issue', 'number=3&orderby=ID&order=DESC&hide_empty=0') as $cat) : ?>
     <a href="<?php echo get_term_link($cat->slug, 'issue'); ?>"><img width="300" height="400" src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
     <?php endforeach; ?>
    Moderator bcworkz

    (@bcworkz)

    Looks good!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_term_by and variables (ID of newest created custom category)’ is closed to new replies.