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!
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!
You could use get_the_title() to store the title inside a variable.. the_title() would actually print the title ..
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.
$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. – Dave Romsey 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