2

I want to list all records in one custom taxonomy start with only A or B. Below code is to list all record with all letters.

Here is the code to list all record with one custom taxonomy with all letters in groups. Example, enter image description here

<?php 
// Template Name: Store Template


// get all the stores
$stores = get_terms(APP_TAX_STORE, array('hide_empty' => 0, 'child_of' => 0, 'pad_counts' => 0, 'app_pad_counts' => 1));
// get ids of all hidden stores 
$hidden_stores = clpr_hidden_stores();
$list = '';
$groups = array();


if ($stores && is_array($stores) ) {

    // unset child stores
    foreach($stores as $key => $value)
    if($value->parent != 0)
          unset($stores[$key]);

    foreach($stores as $store)
        $groups[mb_strtoupper(mb_substr($store->name, 0, 1))][] = $store;

    if (!empty($groups)) :

        foreach($groups as $letter => $stores) {
      $old_list = $list;
      $letter_items = false;
            $list .= "\n\t" . '<h2 class="stores">' . apply_filters( 'the_title', $letter ) . '</h2>';
            $list .= "\n\t" . '<ul class="stores">';

            foreach($stores as $store) {
                if (!in_array($store->term_id, $hidden_stores)) {
                    $list .= "\n\t\t" . '<li><a href="' . get_term_link($store, APP_TAX_STORE) . '">' . apply_filters('the_title', $store->name). '</a> (' . intval($store->count) . ')</li>';
          $letter_items = true;
        }
            }   

            $list .= "\n\t" . '</ul>';

      if(!$letter_items)
        $list = $old_list;
        }

    endif;

} else {

    $list .= "\n\t" . '<p>' . __('Sorry, but no stores were found.', 'appthemes') .'</p>';

}
?>
0

You can use preg_match function:

foreach($stores as $store)
    // if $store starts with A or B
    if( preg_match( '/^(A|B|a|b)/', $store ) )
        //I've not checked this one
        $groups[mb_strtoupper(mb_substr($store->name, 0, 1))][] = $store;
  • @Monolo thanks to reply but this code is not showing any result. – Azim Jan 12 '14 at 23:30
  • What is your output with the original code? – Manolo Jan 13 '14 at 9:17
  • output is as attached pics in question. – Azim Jan 15 '14 at 11:42
  • All right. I'm guessing it is because the upper case. I've updated the answer. Try now. – Manolo Jan 15 '14 at 11:47
0

One way to go about it is to use a custom SQL query with Wildcard search and maybe look something like:

SELECT * 
FROM wp_posts
LEFT JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 
LEFT JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id ) 
LEFT JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id ) 
LEFT JOIN wp_terms ON ( wp_term_taxonomy.term_id = wp_terms.term_id ) 
WHERE wp_terms.name LIKE  'a%'
AND wp_term_taxonomy.taxonomy = 'APP_TAX_STORE'
AND wp_posts.post_status =  'publish'
ORDER BY wp_posts.post_title ASC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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