Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to use get_page_by_title() to get the value of a parent custom post type through Gravity Forms. Basically a patient name is stored in a custom post and their patient id is the post title and the new Gravity Form will populate their medications.

On advice from the Types plugin forum, I'm trying to establish the parent of my post type in a hidden field. The basics are working fine, I can send a value to the field like so:

add_filter('gform_field_value_parent_id', 'parent_id');
function parent_id() {
    return "test";
}

This puts "test" in the field fine, however this doesn't do anything:

add_filter('gform_field_value_parent_id', 'parent_id');
function parent_id() {
    $page = get_page_by_title('TESTID', 'patient');
    return $page;   
}

TESTID is the title of the post and the post type is patient, I've tried it with and without the post type but it just won't do anything. This code is in my functions.php and I'm sure I'm missing something obvious but I just don't see it, can someone help please?

share|improve this question

1 Answer 1

You are missing the second parameter in get_page_by_title. See get_page_by_title() reference. When you are testing without explicitly specified $output and $post_type, this function returns the post of type 'page' object by default. So you have to return $page->post_parent for patient's parent page ID:

add_filter('gform_field_value_parent_id', 'parent_id');
function parent_id() {
    // three parameters here
    $page = get_page_by_title('TESTID', 'OBJECT', 'patient');
    // return patient's parent page ID
    if( !is_null($page) )
        return $page->post_parent;
}

P. S. It's a good practice to enable WP_DEBUG in wp-config.php to see all errors and warnings during development stage. Often it helps to understand bottlenecks faster.

share|improve this answer
    
Thanks for your reply, I've tried what you said and I don't know if I'm being too literal but what form do you think OBJECT should take? I assumed it was as you'd written it here but that causes a php error (I've always had debugging turned on, it just didn't give me an error before): Catchable fatal error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\mydoctorschemist\wordpress\wp-includes\formatting.php on line 510 Also the Wordpress Codex show it being used without all three calls codex.wordpress.org/Function_Reference/get_page_by_title. Thanks :-) –  Dave Legassick Jul 17 '12 at 14:03
    
Unfortunately I can't reproduce this error. Everything works fine for me. Regarding get_page_by_title: it has one required parameter: $page_title. Most likely function will throw an error without it. –  Max Yudin Jul 17 '12 at 15:42
    
May be try to pass a value to your function like this: function parent_id($value). –  Max Yudin Jul 17 '12 at 15:44
    
Nothing's working! Don't you just hate it when this happens! Any other ideas bud? As it is debugging is giving me nothing, do you perhaps know another way of checking this we can try? Oh one other thing when I try to use 'return $page->post_parent' or -> anything it gives me this error: Notice: Trying to get property of non-object in C:\xampp\htdocs\mydoctorschemist\wordpress\wp-content\themes\MyTheme\functions\g‌​forms.php on line 69 –  Dave Legassick Jul 17 '12 at 16:03
    
1. I'll try to reproduce this error one more time. 2. You get this notice when there is no post with such title. I've added one more line to the code above to eliminate this notice. –  Max Yudin Jul 17 '12 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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