If you want to fetch wordpress posts which is starts with specific word or alphabet. That is really easy to achieve in wordpress.

Article for fetch wordpress posts which starting with specific word.

Just follow my steps:

Open your functions.php file from your wordpress theme folder.

add_action( 'posts_where', 'starting_word' );
function starting_word( $sql ){
 global $wpdb;
 $starting_word = get_query_var( 'starting_word' );

 if( $startswith ){
 $sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $starting_word.'%' );

 return $sql;

Open your index.php file or any file where you want show the wordpress posts which is starting with specific word or alphabet.

Use following code in that file.

<?php  query_posts('paged='.$paged.'&starting_word=wordpressapi&category_name=Your Category&posts_per_page=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>

<br />
<?php the_content(); ?>
<?php endwhile; ?>

fetch wordpress posts which starting with specific word

fetch wordpress posts which starting with specific word

Written by Purab

My name is Purab, Here is my Personal Blog: wpapi.com. I contributor of many wordpress plugins and Drupal Modules. I am core developer of Drupal. Connect to on LinkedIn or Follow @purab on Twitter or Follow me on Google+ or Write Mail to purabdk@gmail.com or Chat with me on Skype : purabdk

This article has 3 comments

  1. Lomaski Reply

    well, looks nice but it isn’t working for me i got the “Fatal error: Call to a member function have_posts() on a non-object in C:\xampp\htdocs\riodesign\wp-content\themes\riodesign\category-25.php on line 70”

    line 70: if(have_posts()): while ($my_query->have_posts()) : $my_query->the_post();

    any ideias?

    • sony Reply

      If you are try to use the code in index.php then there is no issue. Now you are trying to use the code in category-25.php file means you are trying use the code the only on category page for only category id 25. how you are writing the code please tell me detail then I will help you..

  2. Josef Reply


    I am using WP 3.0.1, and my theme index.php looks like this:

    Where in this template index.php can I write the code shown above ?


Leave a Comment

Your email address will not be published. Required fields are marked *