0

I have a custom WordPress post type called 'events', which includes date and description fields. I've listed them successfully on a page with their permalinks, which results in a link ending in:

/events/ingenuity-2014-kick-off-reception/?title=Ingenuity%202014%20Kick-Off%20Reception

So I'm passing the title in the URL, and then have this code in single.php:

<?php $title = $_GET['title']?>

        <div id="content" class="content" role="main">

            <?php
            $args = array(
            'posts_per_page' => -1,
            'post_type' => 'events',
            'order' => 'asc'
            );
            $eventslist = new WP_Query( $args );
            while ($eventslist->have_posts()) : $eventslist->the_post(); 

            $actual_title = the_title();

            if($title==$actual_title) {

            $description = get_field('description');
            $date = get_field('date');

             ?>

             The description:
             <?php echo $description ?>
             <?php echo $date ?>
            </div>

            <?php } ?>

For some reason, the resultant single.php page containing the above code is simply spitting out the titles for every post in the events class. I'm having no luck echoing the description based on only the specific title for the post in question.

Is it the %20 that is messing me up? I thought GET would deal with those. Is it something else I'm missing? I'm really stumped... thanks for any help!

1 Answer 1

1

Using the_title() without any arguments just echoes the title. Since the echo is completing successfully, your $actual_title variable is TRUE. In PHP, any string == TRUE. If you used ===, you wouldn't have this false positive issue.

To correct this, use the_title('', '', FALSE) or get_the_title() to return the title to a value within PHP.

Sign up to request clarification or add additional context in comments.

2 Comments

I see the problem. You need to use the_title('', '', FALSE) to return it as a variable. The way you're using it, it just echoes the_title().
Thanks so much, @cpilko--total lifesaver!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.