I am building a custom loop where I want to display post content only if its title matches a specific string. But I am out of luck on getting this done right.

Here is my code so far.

$args = array(
'post_type' => 'property',
'posts_per_page' => -1

$query = new WP_Query( $args );

if ( $query->have_posts() ) {

// The Loop
while ( $query->have_posts() ) {

    if( in_array( 'California', get_the_title() ) ){

        $post = get_page_by_title( get_the_title() );

        /*not sure how to move forward*/




Any help or correct guidance would be highly appreciated.

Thanks in advance.

if (strpos($post->post_title,'string_it_needs_to_match') === true){
echo the_content();


to explain

strpos - method of comparing strings

=== makes sure strings are identical

| improve this answer | |
  • Will it echo the content of the post to which we are matching the string? – bakar Nov 11 '16 at 15:56
  • it should post is set by your first line inside the if statement – Artem Ankudovich Nov 11 '16 at 15:59

Rather than comparing the title inside the loop, you could change your query so that it searches by the title. Other people have done stuff like this:

global $wpdb;
$posts = $wpdb->get_results( 
    $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_title LIKE '%s'"),   
    '%'.$title.'%' );

Than you'll get post object in this form:

foreach ( $posts as $p ) 
    $post = get_post( $p );
    //run your output code here
| improve this answer | |

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.