I
I
Ivseti2021-11-01 20:43:28
WordPress
Ivseti, 2021-11-01 20:43:28

How to find the Misc category in an array in WordPress?

Actually, I've been trying to find the non-removable Misc category in the array all day, since I need to hide it. I will sign, everything in stages: I

brought out a list of all categories with a hook:

function get_categories_product($categories_list = "", $parent_id = 0, $inumberPlus = 0) {

  $get_categories_product = get_terms("product_cat", [
    "orderby" => "name", // Тип сортировки
    "order" => "ASC", // Направление сортировки
    "hide_empty" => 0, // Скрывать пустые. 1 - да, 0 - нет.
    "hierarchical" => 1,
    "parent" => $parent_id
  ]);

  

  if(count($get_categories_product) > 0) {

    if($parent_id == 0) {

      $categories_list .= '<ul class="main_categories_list">';

      $inumberPlus = 0;

    } else {

      $inumberPlus++;

      $categories_list .= '<ul class="sub_categories_list sub_categories_list_'.$inumberPlus.'">';

    }

    foreach($get_categories_product as $categories_item) {

      $categories_list .= '<li><a href="'.esc_url(get_term_link((int)$categories_item->term_id)).'">'.esc_html($categories_item->name).'</a>';
      $categories_list .= get_categories_product("", $categories_item->term_id, $i);
      $categories_list .= '</li>';
    }

    $categories_list .= '</ul>';
  }

  return $categories_list;
}


At the top of the list, the Misc category is just displayed, and I started looking for it in the array.
<?php 


$getMassivmenu = get_terms("product_cat");

if (in_array("Агрохимикаты", $getMassivmenu)) {
    echo "Нашёл Агрохимикаты";
}
if (in_array("Misc", $getMassivmenu)) {
    echo "Нашёл Misc";
} else {echo "Ничего не нашел";}


print('<pre>');
var_dump ($getMassivmenu);
print('</pre>');
?>


I deduced everything that is in the array, the array, as I understand it, is in the object (WP_Term) object ... which added more difficulties to me. Through var_dump I saw the main data, but for some reason it didn’t display everything for me, presumably only those categories where there are goods, the Misc category (the WC category cannot be deleted) is not there.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anonimmus, 2021-11-01
@Ivseti

Misc is the default category. In the wp settings, you can change it, for example, to "main directory" and then delete misc.

A
Artem Zolin, 2021-11-01
@artzolin

Misc is what kind of entity? Title? Then you need to get an array of headers and search in them

$getMassivmenu = get_terms(
  'taxonomy' => 'product_cat',
  'fields' => 'names'
);

if ( in_array( 'Агрохимикаты', $getMassivmenu ) ) {
  echo 'Нашёл Агрохимикаты';
}
if ( in_array( 'Misc', $getMassivmenu ) ) {
  echo 'Нашёл Misc';
} else {
  echo 'Ничего не нашел';
}

In order not to suffer, add this function to your theme and print variablesvardump( $var )
function vardump( $var ) {
  if ( current_user_can( 'manage_options' ) ) {
    echo '<pre>';
      var_dump( $var );
    echo '</pre>';
  }
}

D
Dmitry_Novik, 2021-11-02
@Dmitry_Novik

You can look through the "Admin" in the categories of its id, and then in get_terms('exclude' =>id).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question