If you look at the documentation for get_term_by()
, you’ll see that it:
Will return false if
$taxonomy
does not exist or$term
was not found.
You need to account for this possibility in your code by checking the value of $term
. You’ll also note from the documentation that get_term_by()
does not return a WP_Error
, so is_wp_error()
is not useful. This is what you need:
$term = get_term_by( 'name', $name, $tax );
return $term ? $term->term_id : false;
The specific error you’re seeing is because if $term
is false
then $term->term_id
is invalid code.
Related Posts:
- What does ‘super’ do in Python? – difference between super().__init__() and explicit superclass __init__()
- What is Inversion of Control?
- What is an example of the Liskov Substitution Principle?
- What __init__ and self do in Python?
- When should you use a class vs a struct in C++?
- C++ error ‘Undefined reference to Class::Function()’ [duplicate]
- What’s the difference between a method and a function?
- How do I implement interfaces in python?
- What is polymorphism, what is it for, and how is it used?
- java – invalid method declaration; return type required [duplicate]
- java – invalid method declaration; return type required
- Is C++ an Object Oriented language?
- Difference between abstraction and encapsulation?
- What are metaclasses in Python?
- extends class and implements interface in java
- What is a “driver class”?
- What is difference between functional and imperative programming languages?
- How can I create a copy of an object in Python?
- What is polymorphism in Javascript?
- When should I be using classes in Python?
- How to create a subclass in C#?
- How would one write object-oriented code in C?
- How to get a JavaScript object’s class?
- Inheritance vs. Aggregation [closed]
- What is the difference between an Instance and an Object?
- What does ‘low in coupling and high in cohesion’ mean
- What is an example of the Single Responsibility Principle?
- Will WordPress become completely OOP?
- How to name files of namespaced classes?
- What is the best way to instantiate a class of a plugin in your WordPress theme?
- problem with implementing widget via the_content()
- Load classes using spl_autoload_register
- UML diagrams of WordPress
- Static vs Dynamic methods in WordPress
- Autoloader not finding classes from my plugin
- Integration tests test script enqueue/register fails
- Error of “Call a member function on non object” while var_dump get correct result
- How pass args to wp_list_comments callback?
- Does &$this is really disallowed to use anywhere?
- Don’t filters violate the a class’ local variables visibility rules?
- Admin submenu does not call function to load the page
- Using Geo Data Store Plugin Code
- How to wait for WordPress Core to load when writing OOP?
- OOP – from plugin add new object and call static method in another file
- How to get parameters with add_filter with a static method?
- Understanding Python super() with __init__() methods [duplicate]
- Difference between virtual and abstract methods
- Error “C++ requires a type specifier for all declarations whilst defining methods”
- How to implement a binary search tree in Python?
- Already defined in .obj – no double inclusions
- How to call a parent class function from derived class function?
- Why do some classes require main methods and others do not?
- Can you pass-by-reference in R?
- How do you implement a class in C?
- Does ECMAScript 6 have a convention for abstract classes?
- Explaining Python’s ‘__enter__’ and ‘__exit__’
- Using OOP in themes
- Get menu object from theme_location
- __NAMESPACE__ with register_activation_hook
- How Can I Add a Filter to Class Instance Only?
- Adding an admin page – OOP approach not working
- Plugin Architecture/Design Pattern – is better to use a private Observer/Mediator Pattern for plugin subclasses or WP add_action?
- scheduled event not getting executed
- Using the media uploader in a custom plugin
- Using a front controller in a WordPress plugin, any suggestions?
- How to run query inside a class with namespace?
- Load comments per post on click with AJAX
- Creating a custom post type using a object oriented approach?
- Shortcode Inside Class Not Working
- restore_current_blog required after switch_to_blog, if I use that in a function?
- Registering and using actions which return results in a Plugin class
- Most efficient way to use classes to create admin pages using Settings API
- Basic Object Oriented plugin question
- trouble calling an import class from an action in a plugin before wp_cron executes
- How can I unhook this action that was added via $this?
- Why is my custom post type not being activated on plug-in activation?
- Built a second plugin but it overwrote the first one
- How to access OOP plugin function inside themes or other plugin
- OOP plugin not working
- How to call a function from a shortcode function in an oop plugin
- Use PHP Class in WordPress functions
- WordPress plugin blog creation
- Woocommerce functions in custom class, avoid errors
- Proper use of Output Buffer for a whole php clas
- Calling WordPress hook from within a class: call to undefined function error
- Class called in template, AJAX not registering
- How is construct function working even when variable is assigned null value?
- Code works on a page but not in a widget
- What is wrong with assigning values to a WP_List_Table class?
- wpdb insert working in one function, but not another
- The writing ( with foreach) of the keys of an array of objects to a new array produces fatal error
- How to remove a filter that is an anonymous object?
- OOP – add_action
- Why can wordpress not find the actions I add in my constructor?
- Plugin won’t activate, fatal error (widget class not found)
- How do I create Widget within plugin that uses its own class?
- Populate Array with values from another arrray [closed]
- Function not being called on form submit, only blank admin-post.php page
- How to get data with Select AJAX PHP
- How to use filter in this situation, can not modify the structure using filter