Assuming the letters are links, then put the letter into the URL as a parameter. E.g.:
http://example.com/brands/?letter=N
Then, in the code (inside whichever template file will be used here), you need to query the DB for posts/brands beginning with ‘N’.
You can either create a taxonomy for letters
(https://codex.wordpress.org/Function_Reference/register_taxonomy) and assign each post to a letter
(where the taxonomy’s terms are all the letters`), and then use one of the following:
There are several ways:
1 – WP_Query
2 – get_posts()
.. Several other methods.
Here is how you can do it with WP_Query
:
$args = array(
'post_type' = 'brands', //assuming the post type slug is 'brands'
'posts_per_page' = -1,
'tax_query' => array(
array(
'taxonomy' => 'letters',
'field' => 'slug',
'terms' => 'N', // sanitize_text_field($_GET['letter'])
),
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo 'no brands beginning with N;
}
Or, instead of having to create a taxonomy for letters, you could do this (a more-custom query, using $wpdb
):
global $wpdb;
$request = sanitize_text_field($_GET['letter']);
$results = $wpdb->get_results(
"
SELECT * FROM $wpdb->posts
WHERE post_title LIKE '$request%'
AND post_type="brands"
AND post_status="publish";
"
);
if ( $results )
{
foreach ( $results as $post )
{
setup_postdata ( $post );
the_title();
}
}
else
{
echo 'no brands beginning with 'N';
}
More on $wpdb
– https://codex.wordpress.org/Class_Reference/wpdb
More on WP_Query
– https://codex.wordpress.org/Class_Reference/WP_Query