• Resolved 000000000

    (@pealo86)


    I have a custom taxonomy set up like so:

    register_taxonomy(
    	'locations',
    	'franchisee',
    	array(
    		'label' => __( 'Locations' ),
    		'sort' => true,
    		'args' => array( 'orderby' => 'term_order' ),
    		'rewrite' => array( 'slug' => 'location' )
    	)
    );

    I then have several posts with ‘Chester’ applied as a location.

    What I’m trying to do however, is to get the ID of the term ‘Chester’ using only the value ‘Chester’, if that makes sense.

    I’ve read up on the get_term_by() function however cannot see anything relating to my issue in particular

Viewing 5 replies - 1 through 5 (of 5 total)
  • Does this not work?

    <?php get_term_by( 'name', 'Chester', 'locations' ) ?>

    Thread Starter 000000000

    (@pealo86)

    Thanks! I’ve tried that, however it gives me this error when trying to echo:

    Catchable fatal error: Object of class stdClass could not be converted to string in /var/www/vhosts/uaf/httpdocs/wp-content/themes/uaf/page-franchisees.php on line 7

    https://codex.www.ads-software.com/Function_Reference/get_term_by

    try:

    <?php $termID = get_term_by( 'name', 'Chester', 'locations' )->term_id;
    echo $termID; ?>

    or:

    <?php $term = get_term_by( 'name', 'Chester', 'locations' );
    echo $term->term_id; ?>

    get_term_by(...) doesn’t return the ID.

    $output
    (string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N

    Default: OBJECT

    https://codex.www.ads-software.com/get_term_by

    It returns an ARRAY or OBJECT that contains the ID– OBJECT by default. If you tried to echo that you would get the error you posted. Try var_dump(get_term_by( 'name', 'Chester', 'locations' )) and see if you are getting the information you need.

    Thread Starter 000000000

    (@pealo86)

    Thanks guys, sorted it with:

    $loc = get_term_by( 'name', 'Chester', 'locations' );
    $loc = $loc->term_id;

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get ID Term from Value’ is closed to new replies.