I have a custom post called foundation_firms
, from which I'd like to create an array of letters, each letter representing that there exists a post starting with that letter.
I've been trying to use in_array()
to avoid adding a letter to the array if it already is in the array, however that approach hasn't been working for me. Anyone have any idea why? Here's my code thus far:
$alphas = range('A', 'Z');
$post_alphas = [];
$post_args = array(
'post_type' => 'foundation_firms',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1,
'orderby' => 'title',
);
$firm_query = new WP_Query($post_args);
if($firm_query->have_posts()){
while($firm_query->have_posts()){
$firm_query-> the_post();
$title = get_the_title();
echo $title.'<br/>';
$title_alpha = $title[0];
if(!in_array($post_alphas, $title_alpha)){
$post_alphas[] = $title_alpha;
}
}
}
$final_alpha = array_intersect($post_alphas, $alphas);
echo '<pre>'.var_dump($final_alpha).'</pre>';
Basically this just gives me an array of the first letter of each post, duplicates and all. Isn't the in_array()
function supposed to eliminate that?
Thanks in advance to any takers here.