Support » Themes and Templates » get_post_custom but no get_page_custom

  • I’m trying to figure out how to retrieve a custom field value for a page (not a post).

    • I have a page in which I defined a custom field via the wp-admin edit page
    • The page uses a custom template I wrote
    • In the template I am not accessing any posts. There is no query for posts, no “the loop” and no $post_id
    • I would like to retrieve the page custom field value but I cannot find an API.
    • I thought since there is “get_post_custom()” function there should be a “get_page_custom()” but evidently there is not
    • I tried calling get_post_custom() (passing no args) but that is empty

    Is there an equivalent of a “get_page_custom()”?

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • @michael – get_post_custom works for both pages and posts.

    You do need to have an ID though to pass to it.

    If you are not in a loop, or on a page where the global $post has been set for you – you will need to get the page ID by some other means.

    e.g. if you have the page slug:

    function get_ID_by_slug($page_slug) {
     // from: http://erikt.tumblr.com/post/278953342/get-a-wordpress-page-id-with-the-slug
    $page = get_page_by_path($page_slug);
        if ($page) {
            return $page->ID;
        } else {
            return null;
        }
    };
    
    $page_ID = get_ID_by_slug ('my-slug');
    
    $custom = get_post_custom($page_ID);

    @michael – get_post_custom() works for both pages and posts.

    You do need to have an ID though to pass to it.

    If you are not in a loop, or on a page where the global $post has been set for you – you will need to get the page ID by some other means.

    e.g. if you have the page slug:

    function get_ID_by_slug($page_slug) {
     // from: http://erikt.tumblr.com/post/278953342/get-a-wordpress-page-id-with-the-slug
    $page = get_page_by_path($page_slug);
        if ($page) {
            return $page->ID;
        } else {
            return null;
        }
    };
    
    $page_ID = get_ID_by_slug ('my-slug');
    
    $custom = get_post_custom($page_ID);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_post_custom but no get_page_custom’ is closed to new replies.