Show meta box only for top parent page ID

Support MB Include Exclude Show meta box only for top parent page ID

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #6982
    HazmiHazmi
    Participant

    Hi,

    i'm using include/exclude addon, where i have a custom param (function) to show this metabox only for pages, that have some top parent page ID - it's ok. Problem is, when i save the post (the content from custom field is empty) - so it seems, that my function for test top parent page id is executed early before WP knows ID for current saving post.

    How can i solve this?

    #6984
    Anh TranAnh Tran
    Keymaster

    Can I see your function for test top parent page ID? Can you post your code?

    #6988
    HazmiHazmi
    Participant

    This function shows me a metabox for pages, that have top parent page id "429".

    function parent_page_id_check(){
    	if( is_admin() && isset($_GET['post']) ){
    		$id = $_GET['post'];
    		$parents = get_post_ancestors($id);
    		$id = ($parents) ? $parents[count($parents)-1] : $id;
    		return ($id == 429) ? true : false;
    	}
    }
    #6990
    Anh TranAnh Tran
    Keymaster

    Why don't you use parent rule? It's supported by default 😉

    #6992
    HazmiHazmi
    Participant

    Because i have not only a single subpage (page - subpage - subsubpage ..)

    #7005
    HazmiHazmi
    Participant

    So how is the correct using this addon for my purpose?

    #7011
    Anh TranAnh Tran
    Keymaster

    Hi again,

    I found the problem for your situation. Your check doesn't run when post is updated, because there's no $GET['post']. In that case, you need to get post ID via $_POST. Here is the code:

    function prefix_parent_page_id_check() {
    	$parents = get_post_ancestors( prefix_get_post_id() );
    	return $parents && 901 == array_pop( $parents );
    }
    
    function prefix_get_post_id() {
    	if ( isset( $_GET['post'] ) ) {
    		return intval( $_GET['post'] );
    	}
    	if ( isset( $_POST['post_ID'] ) ) {
    		return intval( $_POST['post_ID'] );
    	}
    
    	return false;
    }
    #7013
    HazmiHazmi
    Participant

    Hi,
    aaaah super - it's working! Thank You Anh.

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Show meta box only for top parent page ID’ is closed to new replies.