My page title is This is my title
, if I try and retrieve the ID from the title like this then it does not work:
$mytitle = 'This is my title';
$mytitle2 = get_page_by_title( $mytitle, OBJECT, 'mycustompost' );
print_r($mytitle2);
But if I do this it does work:
$mytitle2 = get_page_by_title( 'This is my title', OBJECT, 'mycustompost' );
print_r($mytitle2);
What gives? Does get_page_by_title
not accept variables?
Man this was an annoying problem for me as well because I a function passing the variable and when tested it showed a value (with special characters, like “&”. If I reset the value statically it worked but otherwise same issue. I ran html_entity_decode() on the variable and it now works perfectly so thought I would pass on in case it helps someone. Here was my function:
Retrieve post/page/custom-post-type id it works fine
Note:enable WP_DEBUG in wp-config.php to see all errors and warnings and use var_dump to check values in variables.
For more info: read get_page_by_title() not returning anything