Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need help in making this work in the latest version of wordpress. On Version 3.6.1 and down works well when I tested but when i decide to update WordPress well it breaks it.

Ex: Version 3.6.1 looks like this

A|B|C

(A)    (B)    (C)
Apple  Bat    Cat

Ex: The latest version 3.9.1 looks like this

A|B|C

(A)    (B)    (C)
Apple  Apple  Apple
Bat    Bat    Bat
Cat    Cat    Cat

This is the code that works on 3.6.1 can some one help me make it work on the latest version?

<?php 
$a = array('name__like' => "a", 'order' => 'ASC', 'title_li' => '0', 'use_desc_for_title' => '0');
$b = array('name__like' => "b", 'order' => 'ASC', 'title_li' => '0', 'use_desc_for_title' => '0');
?>
<div class="menu-alpha">
<ul>
<li><a href="#A">A</a></li>
<li><a href="#B">B</a></li>
</ul>
</div>


<div id="left-side">
<div class="alpha"><a name="A">A</a></div>
<ul>
<?php wp_list_categories($a); ?>
</ul>
<div class="alpha"><a name="B">B</a></div>
<ul>
<?php wp_list_categories($b); ?>
</ul>
share|improve this question

2 Answers 2

You are using wp_list_categories() function with the parameter name__like but this parameter is not valid for this function (I don't think your code works as you describe in WP 3.6+). You can use get_terms. For example:

 <?php

 function cyb_get_categories_list_name_like($letter) {

     $categories = get_terms( 'category', array('name__like' => $letter) ) ;

     $list = '';  

     if( !empty( $categories ) && !is_wp_error( $categories ) ) {

         $list = '<ul>';

         foreach( $categories as $cat ) {
              $list .= '<li><a href="'.get_term_link($cat).'">'.$cat->name.'</a></li>';
         }

         $list = '</ul>';

     }

     return $list;

 }

 <div class="menu-alpha">
      <ul>
           <li><a href="#A">A</a></li>
           <li><a href="#B">B</a></li>
      </ul>
 </div>

 <div id="left-side">
      <div class="alpha"><a name="A">A</a></div>
      <?php cyb_get_categories_list_name_like('a'); ?>
      <div class="alpha"><a name="B">B</a></div>
      <?php cyb_get_categories_list_name_like('b'); ?>
 </div>

 ?>

As Pieter Goosen pointed, it is need a filter to make the name__like parameter match only the first letter of the term:

 //Credit here @s_ha_dum
 function old_style_name_like_wpse_123298($clauses) {
      remove_filter('term_clauses','old_style_name_like_wpse_123298');
      $pattern = '|(name LIKE )\'%(.+%)\'|';
      $clauses['where'] = preg_replace($pattern,'$1 \'$2\'',$clauses['where']);
      return $clauses;
 }
 add_filter('terms_clauses','old_style_name_like_wpse_123298');
share|improve this answer
    
When you added this part $categories = get_terms( 'category', array('name__like' => $letter) ) ; where is says category do i add something there? Take note am still new at the whole WordPress code part. –  Constantino_A_ Jun 14 at 7:26
    
"category" is passed to get the terms from the "category" taxonomy, which is one of the Wordpress core taxonomies. –  cybmeta Jun 14 at 9:43

You should rather use get_terms here as get_terms support the name__like parameter. Unfortunately the name__like parameter was changed in wordpress version 3.7. Pre 3.7, it only matched a term by first letter, now it matches all the letters in the name, not just the first letter.

Credit for the first part of this question should go to @s_ha_dum for this solution. What his function does is changing the filter used so that the name__like parameter again only match the first letter of the term name. Many thanks s_ha_dum. I quote from his answer to this this question.

You will need to filter the query, which you can do with the terms_clauses hook

The credit for the second part of the solution should go me. What I did here is to make your code more dynamic and cut down completely on unnecessary code. Here I took the php function range() to get a list of all letters of the alphabet, and then use that letters to feed back into name__like to get the terms (categories)

Here is the complete solution

<?php 
//Credit here @s_ha_dum
function old_style_name_like_wpse_123298($clauses) {
  remove_filter('term_clauses','old_style_name_like_wpse_123298');
  $pattern = '|(name LIKE )\'%(.+%)\'|';
  $clauses['where'] = preg_replace($pattern,'$1 \'$2\'',$clauses['where']);
  return $clauses;
}
add_filter('terms_clauses','old_style_name_like_wpse_123298');

//Credit here me @PieterGoosen
foreach (range('A', 'Z') as $number){

    $args=array(
    'name__like' => $number,
    );

    $terms = get_terms( 'category', $args);

    if ( !empty( $terms ) && !is_wp_error( $terms ) ){
        echo '<li>' . $number . '</li>';

        foreach ( $terms as $term ) {

                echo '<p>' . $term->name . '</p>';

        }
    }
}

You can just now add your html mark-up as needed and modify it as you need

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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