get_page_by_title not working when used with a variable

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:

public function get_id_by_code($coupon){
    $test = get_page_by_title( html_entity_decode( $coupon ), OBJECT, 'coupon' );
    return $test->ID;
}

Leave a Comment