1

I write a plugin. it has the ability to filter post by category and also its subcategories. I get some error and I don't know why I get it! I commented on the errors. Could you help me?

function wf_get_all_category( $taxonomy = 'category', $parent = 0, $hide_empty = true ) {
    $result = [];
    $terms  = get_terms( [
        'taxonomy'   => $taxonomy,
        'hide_empty' => $hide_empty,
        'parent'     => $parent,
    ] );
    if ( count( $terms ) ) {
        foreach ( $terms as $term ) {
            $t_id        = $term->term_id;
            $term_fields = get_option( "taxonomy_$t_id" );
            $filter_show = $term_fields['filter_show'];
            if ( $filter_show != 1 ) {
                $result[] = $term;
            }
        }
    }

    return ( $result );
}

function wpf_cats( $cats = [], $taxonomy = 'category' ) {
    $count    = 0;
    $out_html = '';
    foreach ( $cats as $cat ) {

        if ( isset( $cat->parent ) && !is_numeric( $cat->parent ) ) {
            continue;
        }

        $cat_list = wf_get_all_category( $taxonomy, $cat->parent );  //Trying to get property 'parent' of non-object
        if ( count( $cat_list ) ) {
            $ajax_class = $cat->parent != 0 ? "wpf-filter-ajax" : ""; //Trying to get property 'parent' of non-object
            $out_html   .= "<select id=['parent-$cat->parent'] class='wp-filter " . $ajax_class . "'>"; //Trying to get property 'parent' of non-object
            $out_html   .= "    <option data-slug='none' value='none'> Chose a Category</option>";
            foreach ( $cat_list as $term ) {
                $out_html .= "<option  data-slug='" 
                . get_term_link( $term ) . "' " . selected( $cat->term_id, $term->term_id, false ) //Trying to get property 'term_id' of non-object
                . " data-taxonomy='" . $term->taxonomy . "' value='" . $term->term_id . "'>" . $term->name . "</option>";
            }
            $out_html .= "</select>";
        }
    }
    return $out_html;
}


add_shortcode( 'me-wp-filter', 'me_wp_filter' );
function me_wp_filter( $html ) {
    $parent_list = false;
    $taxonomy    = "category";
    if ( is_category() ) {
        global $wp_query;
        $cat_obj     = $wp_query->get_queried_object();
        $thisCat     = $cat_obj;
        $parent_list = wpf_parent_list( $thisCat );
        array_push( $parent_list, $thisCat );
        $taxonomy = "category";
    }

    if ( is_single() ) {
        return '';
    }


    $html .= '<div class="woo-filter-select-warp">
             <div class="select-box">
              ';

    $html .= '<span class="wpf-list-box">';

    if ( $parent_list ) {


        $html .= wpf_cats( $parent_list, $taxonomy );
    } else {
        $html .= wpf_cats_first( 0, $taxonomy );
    }

    $key_class = "close";
    $key       = '';
    if ( isset( $_GET['s'] ) && !empty( $_GET['s'] ) ) {
        $key_class = "open";
        $key       = $_GET['s'];
    }
    $html .= '</span>
              <span class="wp-filter-mode_input">
                <div class="' . $key_class . ' form">
                  <input type="text" id="key" placeholder="کلمه کلیدی" value="' . $key . '"/>
                  <div class="after"></div>
                </div>
                <button type="button" id="go-filter" class="material-button ripple widget_submit has-ripple mbtn">فیلترکن!</button></div>
               </span>
          </div>';

    return ( $html );
}
  • You haven't shown us how you're calling wpf_cats and what you are passing it - the results of another get_terms() call? Do you need to read the parent as ["parent"] not ->parent ? – Rup Aug 28 '19 at 11:50
  • @Rup thank you for your reply. I updated codes – Mohammad Aug 28 '19 at 12:59
  • 1
    Thanks but I'm not sure that helps. It would be useful to see what $cats is in wpf_cats. You've shown us that it's the output from wpf_parent_list (or false) but you haven't shown us wpf_parent_list. Is it just the output from a get_terms() call? If it is, then it's not a class so you can use ->parent: you'll have to use ['parent'] instead. – Rup Aug 28 '19 at 13:24
  • "Some error" isn't specific enough. What error exactly? – fuxia Sep 3 '19 at 7:21

Browse other questions tagged or ask your own question.