0

I made 2 plugins (Plugin A and plugin B/custom post type.) which some of the the module on Plugin A need to call taxonomy from PLUGIN B. Everything works fine when both plugin active, but when the Plugin B deactivate, I got this notice:

Notice: Trying to get property of non-object in D:\MYWEB\InstantWP_4.3.1\iwpserver\htdocs\wordpress\wp-content\plugins\NYPLUGIN\myplugin.php on line 50

Line 50 looks like this:

$option_value_event[] .= $cat->name;

Here the code:

 //Get all EVENT categories
 $args = array(
        'type'                     => 'post', 
        'child_of'                 => 0, 
        'parent'                   => '', 
        'orderby'                  => 'date', 
        'order'                    => 'ASC', 
        'hide_empty'               => 1, 
        'hierarchical'             => 1, 
        'exclude'                  => '', 
        'include'                  => '', 
        'number'                   => '', 
        'taxonomy'                 => 'catevent', 
        'pad_counts'               => false 
 ); 
 $categories_event = get_categories($args);
 $option_value_event = array();

 //Extract all EVENT categories
 foreach ( $categories_event as $cat ){ 
 $option_value_event[] .= $cat->name;
 }

Sometime my user dont want to use Plugin B. Is there any way to deactivate PLUGIN B without break PLUGIN A?

Is there a quick fix to resolve these error?

Really appreciate for any help

Thank you

2 Answers 2

3

You will either need to do two things

  • Check for an empty array AND a WP_Error object

  • Check if the taxonomy exists (which get_categories() and get_terms() already does) before your execute your code and still check for an empty array

FEW NOTES TO CONSIDER

  • get_categories() uses get_terms(), you you could use get_terms()

  • You do not need to set arguments if you use it's default values

  • date is not a valid value for orderby in get_terms(). Valid values are name, slug, term_group, term_id, id, description and count

  • The type parameter in get_categories() does not relate to the post type, but to the taxonomy type. This parameter used to accept link as value and where used before the introduction of custom taxonomies in WordPress 3.0. Before WordPress 3.0, you could set type to link to return terms from the link_category taxonomy. This parameter was depreciated in WordPress 3.0 in favor of the taxonomy parameter

SOLUTION

//Get all EVENT categories
$categories_event = get_terms( 'catevent' );

$option_value_event = array();  

// Check for empty array and WP_Error
if (    $categories_event
     && !is_wp_error( $categories_event )
) {
    //Extract all EVENT categories
    foreach ( $categories_event as $cat ){ 
        $option_value_event[] = $cat->name;
    }
}

EDIT

I cannot see why the above code would not work. You can try the following as a test

if ( taxonomy_exists( 'catevent' ) ) {
    //Get all EVENT categories
    $categories_event = get_terms( 'catevent' );

    $option_value_event = array();  

    // Check for empty array and WP_Error
    if (    $categories_event
         && !is_wp_error( $categories_event )
    ) {
        //Extract all EVENT categories
        foreach ( $categories_event as $cat ){ 
            $option_value_event[] = $cat->name;
        }
    }
}

That should work, if not, then you have a serious error somewhere in one of your plugins

6
  • your note is very useful. I read again for codex and use it Thank a lot. But unfortunately, the notify still exist.
    – Mailmulah
    Mar 4, 2016 at 12:20
  • Sorry, there where two silly mistakes on my side. Have corrected it, please check my code again. It should work now Mar 4, 2016 at 12:30
  • Same like before, the error still in there. The problem came from this line: $option_value_event[] .= $cat->name;
    – Mailmulah
    Mar 4, 2016 at 12:41
  • See my edit. It should work now, if not, then you have a serious error somewhere else which is causing this one Mar 4, 2016 at 12:49
  • $option_value_event[] .= $cat->name should be $option_value_event[] = $cat->name Mar 4, 2016 at 12:50
0

Well, a simple solution is to check if whatever is returned is empty. Always throw in these conditionals to ensure you have what you need before doing any kind of concatenation or evaluation. A simple PHP function such as is_empty() should suffice:

//Get all EVENT categories
$args = array(
        'type'                     => 'post', 
        'child_of'                 => 0, 
        'parent'                   => '', 
        'orderby'                  => 'date', 
        'order'                    => 'ASC', 
        'hide_empty'               => 1, 
        'hierarchical'             => 1, 
        'exclude'                  => '', 
        'include'                  => '', 
        'number'                   => '', 
        'taxonomy'                 => 'catevent', 
        'pad_counts'               => false 
); 
$categories_event       = get_categories( $args );
$option_value_event     = array();

//Extract all EVENT categories
if( ! empty( $categories_event ) ) {
    foreach( $categories_event as $cat ) { 
        $option_value_event[] .= $cat->name;
    }
}
6
  • apologize, just re-check and the problem still show up.
    – Mailmulah
    Mar 3, 2016 at 23:15
  • @Mailmulah On the same line? I imagine you'll have issues with $option_value_event later on too you'll need to ensure that's not empty also. ! empty( $option_value_event ) and probably other places.
    – Howdy_McGee
    Mar 3, 2016 at 23:16
  • Yes, in the same line. The problem is in $option_value_event
    – Mailmulah
    Mar 3, 2016 at 23:23
  • Can you print out the contents of your array? print_r( $categories_event ); - in theory if catevent doesn't exist it should be empty but I'm curious what it holds. If it's large you can throw the gist of it into a pastebin
    – Howdy_McGee
    Mar 3, 2016 at 23:25
  • I'm on mobile but I don't see where you added the conditional. And I don't need the code, just the contents of what get_categories() is returning. It should be empty.
    – Howdy_McGee
    Mar 3, 2016 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.