0

I want to use the the_title() function to get the title of a post and then reference that title in an array. Code is as follows:

    <?php
            $title = array( 
                the_title()
            );
            $args = array(
                'post_type' => array( 'questions' ), 'content' => array( $title )
            );

So I want to take the post title and use the $args variable to find posts that have post type = 'questions' and have the current post's title as their value for the 'content' taxonomy.

Currently it isn't working. The post I'm testing on has title 'Book1'and it works when I change the $args line to:

$args = array(
                'post_type' => array( 'questions' ), 'content' => array( 'Book1' )

But with the code I listed first, it doesn't work...

EDIT: yep, it was simple: just needed to change it to 'content' => $title because $title is already an array. Thanks all!

3
  • 1
    How is the $args array being used? Also, where is this code being used? Int the loop? Within a function hooked somewhere? Please edit the original question to show this code too. @ngcodex is right about using get_the_title() instead of the_title(), since the latter will echo output immediately. Commented Jan 29, 2017 at 20:07
  • $title is already an array - therefore this should be 'content' => $title
    – Michael
    Commented Jan 29, 2017 at 21:05
  • Glad to hear that you fixed the problem, Rob. Please post your solution as an answer to your question, then stop by in a couple of days and click the checkmark next to the answer to accept it. This will close the question out of the unanswered question queue. Thank you! Commented Jan 30, 2017 at 7:44

2 Answers 2

0

You could use get_the_title() to store the title inside a variable.. the_title() would actually print the title ..

1
  • Ah, thanks! But unfortunately it still doesn't seem to work... have edited my original question to explain a little better
    – Rob Crews
    Commented Jan 29, 2017 at 14:46
0

Answer was simple in the end:

<?php
        $title = array( 
            the_title()
        );
        $args = array(
            'post_type' => array( 'questions' ), 'content' => $title
        );

So I just needed to change 'content' => array( $title) to simply 'content' => $title. This was because $title is already an array!

Thanks everyone for the help.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.