Forums

Trying to query by subject of posts (4 posts)

  1. theshaner
    Member
    Posted 3 years ago #

    I have pages outside of my blog that I put content from the blog on. I have one page where I would like to query the database by looking at the subject line for a particular word or two. Is this possible? Can someone give me an example of how I might do that?

    Thanks!

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    The template tag, query_posts(), will not allow you to filter for posts containing specific words. But you could use this in a query post loop:

    <?php
    $stringtotest=$post->post_title;
    $findstring="cat1";
    $pos = strpos($stringtotest, $findstring);
    if ( $pos !== false ) {
    echo 'this post title contains the string cat1 ';
    }
    ?>

    See: http://us3.php.net/manual/en/function.strpos.php

  3. theshaner
    Member
    Posted 3 years ago #

    Sorry, I am not proficient with DB's in php. Can you show me how I could fit this into what I am using to query?

    Here is what I am using currently. The $sa_reference is my var I set up to hit the cat I need, then I would just need to try and show the last post in that cat that had say the word "football" in the subj.

    `<?php
    query_posts("category_name='$sa_reference'");

    while (have_posts()): the_post(); ?>
    <h2>
    <?php the_title();?>
    </h2>
    Posted by
    <?php echo("<b>"); the_author(); echo("</b>"); echo(" on "); the_date(); echo(" at "); the_time(); ?>

    <?php the_content(); ?>
    <p>
    <?php endwhile; ?>`

    I would really really appreciate it!

  4. MichaelH
    Volunteer
    Posted 3 years ago #

    This adds a new twist:

    show the last post in that cat

    .

    So you want the latest post in that category that has the word football in the title? Didn't really test this but try...

    <?php
    query_posts("category_name='$sa_reference'");
    while (have_posts()): the_post(); ?>
    <?php
    $stringtotest=$post->post_title;
    $findstring="football";
    $pos = strpos($stringtotest, $findstring);
    if ( $pos !== false ) {
    $count++;
    if ($count == 1) {
    ?>
    <h2>
    <?php the_title();?>
    </h2>
    Posted by
    <?php echo("<b>"); the_author(); echo("</b>"); echo(" on "); the_date(); echo(" at "); the_time(); ?>
    <?php the_content(); ?>
    <p>
    <?php
    }
    }
    endwhile; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.