• I am having a problem trying to query WordPress to just show me say a post containing “Word 1” and “Word 2”.

    I would actually like to have this directly in an article (so I would create a new entry then have this information filled right in). My goal is for something like this to happen:

    Query Words Containing “Hello” and “Good Bye”
    Show every article title (and link to the article itself) containing those words in descending order.
    Have each article title wrapped in a

    • before it and
    • after it.

      Is that possible to do? If so, I’d really APPRECIATE some example code! Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • <?php
    // display post title for any post that has both $findtext1 and $findtext2 in the post content
    $findtext1 = 'Welcome';
    $findtext2 = 'first';
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'orderby' => 'date',
      'order' => 'DESC',
      'showposts' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts containing "'.$findtext1.'" and "'.$findtext2.'"';
      while ($my_query->have_posts()) : $my_query->the_post();
        if ( (strpos($post->post_content, $findtext1)!== false) && (strpos($post->post_content, $findtext2)!== false)) {   ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        } //if (strpos
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Test of $findtext1 and $findtext2 is case sensitive.

    Note: recommend upgrading to 2.8.5

    Thread Starter fusiongt

    (@fusiongt)

    Michael you rock! It’s working great. However, one minor thing and that is this part:

    if( $my_query->have_posts() ) {
    echo ‘List of Posts containing “‘.$findtext1.'” and “‘.$findtext2.'”‘;

    It shows the text even if there are no posts (I tried adding some gibberish $findtext1 and $findtext2 and it didn’t have any results except the echo statement still showed).

    Could you help me with that? Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is there a way to get a list of all articles containing “these words” ?’ is closed to new replies.