fetch wordpress posts which starting with specific word

fetch wordpress posts which starting with specific word or alphabet

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

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

3 thoughts on “fetch wordpress posts which starting with specific word or alphabet”

  1. 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?

    1. 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. Hello,

    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 ?

    Regards,
    Josef

Leave a Reply

Your email address will not be published.