Solved

Exclude Certain Pages From Wordpress Metabox In Menus Screen

Posted on 2013-06-05
10
377 Views
Last Modified: 2013-06-15
I have the below piece of code in my functions.php which essentially creates a metabox on the Menus screen (Appearance -> Menus) and populates it with a list of my custom post types and pages.  

There are two pages (not cpt's or posts, but pages) I would like to exclude from this to hide them from my clients who will be accessing the Menus screen.  I tried adding
'exclude'      => '39,967',

Open in new window

to the $recent_args args array to no avail.  

Any ideas exactly how I can remove these two pages from the metabox?

Here is the code (Which could probably use a bit of cleaning up itself, I know):

// Nav Menu
add_action( 'admin_init', array( 'call_someClass', 'init' ) );

/**
 * The Class
 */
class call_someClass {
    const LANG = 'exch_lang';

    public static function init() {
        $class = __CLASS__;
        new $class;
    }

    public function __construct() {
        global $pagenow;
        // Abort if not on the nav-menus.php admin UI page - avoid adding elsewhere
        if ( 'nav-menus.php' !== $pagenow )
                    return;

        $this->add_some_meta_box();
    }

    /**
     * Adds the meta box container
     */
    public function add_some_meta_box(){
        add_meta_box(
            'info_meta_box_'
            ,__( 'Show Pages', self::LANG )
            ,array( $this, 'render_meta_box_content' )
            ,'nav-menus' // important !!!
            ,'side' // important, only side seems to work!!!
            ,'high'
        );
    }

    /**
     * Render Meta Box content
     */
    public function render_meta_box_content() {
        global $_nav_menu_placeholder, $nav_menu_selected_id;

        $post_type_name = 'all-cpts';
        $post_types_list = array_keys(get_post_types( array( 'show_in_nav_menus' => true ), 'array' ));

        // paginate browsing for large numbers of post objects
        $per_page = 50;
        $pagenum = isset( $_REQUEST[$post_type_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
        $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;

        $args = array(
            'offset' => $offset,
            'order' => 'ASC',
            'orderby' => 'title',
            'posts_per_page' => $per_page,
            'post_type' => $post_types_list,
            'suppress_filters' => true,
            'update_post_term_cache' => false,
            'update_post_meta_cache' => false
        );

        if ( isset( $post_type['args']->_default_query ) )
            $args = array_merge($args, (array) $post_type['args']->_default_query );

        // @todo transient caching of these results with proper invalidation on updating of a post of this type
        $get_posts = new WP_Query;
        $posts = $get_posts->query( $args );
        if ( ! $get_posts->post_count ) {
            echo '<p>' . __( 'No items.' ) . '</p>';
            return;
        }

        $post_type_object = get_post_type_object($post_type_name);

        $num_pages = $get_posts->max_num_pages;

        $page_links = paginate_links( array(
            'base' => add_query_arg(
                array(
                    $post_type_name . '-tab' => 'all',
                    'paged' => '%#%',
                    'item-type' => 'post_type',
                    'item-object' => $post_type_name,
                )
            ),
            'format' => '',
            'prev_text' => __('&laquo;'),
            'next_text' => __('&raquo;'),
            'total' => $num_pages,
            'current' => $pagenum
        ));

        if ( !$posts )
            $error = '<li id="error">'. $post_type['args']->labels->not_found .'</li>';

        $db_fields = false;
        if ( is_post_type_hierarchical( $post_type_name ) ) {
            $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
        }

        $walker = new Walker_Nav_Menu_Checklist( $db_fields );

        $current_tab = 'most-recent';
        if ( isset( $_REQUEST[$post_type_name . '-tab'] ) && in_array( $_REQUEST[$post_type_name . '-tab'], array('all', 'search') ) ) {
            $current_tab = $_REQUEST[$post_type_name . '-tab'];
        }

        if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
            $current_tab = 'search';
        }

        $removed_args = array(
            'action',
            'customlink-tab',
            'edit-menu-item',
            'menu-item',
            'page-tab',
            '_wpnonce',
        );

        ?>
        <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
            <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
                <li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'most-recent', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent"><?php _e('Select Page Below'); ?></a></li>
                <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'all', remove_query_arg($removed_args))); ?>#<?php echo $post_type_name; ?>-all"><?php _e(''); ?></a></li>
                <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'search', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-search"><?php _e(''); ?></a></li>
            </ul>

            <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php
                echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
            ?>">
                <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
                    <?php
                    $recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 20) );
                    $most_recent = $get_posts->query( $recent_args );
                    $args['walker'] = $walker;
                    echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $most_recent), 0, (object) $args );
                    ?>
                </ul>
            </div><!-- /.tabs-panel -->

            <div class="tabs-panel <?php
                echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
            ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
                <?php
                if ( isset( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
                    $searched = esc_attr( $_REQUEST['quick-search-posttype-' . $post_type_name] );
                    $search_results = get_posts( array( 's' => $searched, 'post_type' => $post_types_list, 'fields' => 'all', 'order' => 'DESC', ) );
                } else {
                    $searched = '';
                    $search_results = array();
                }
                ?>
                <p class="quick-search-wrap">
                    <input type="search" class="quick-search input-with-default-title" title="<?php esc_attr_e('Search'); ?>" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" />
                    <span class="spinner"></span>
                    <?php submit_button( __( 'Search' ), 'button-small quick-search-submit button-secondary hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-posttype-' . $post_type_name ) ); ?>
                </p>

                <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
                <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
                    <?php
                    $args['walker'] = $walker;
                    echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
                    ?>
                <?php elseif ( is_wp_error( $search_results ) ) : ?>
                    <li><?php echo $search_results->get_error_message(); ?></li>
                <?php elseif ( ! empty( $searched ) ) : ?>
                    <li><?php _e('No results found.'); ?></li>
                <?php endif; ?>
                </ul>
            </div><!-- /.tabs-panel -->

            <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
                echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
            ?>">
                <?php if ( ! empty( $page_links ) ) : ?>
                    <div class="add-menu-item-pagelinks">
                        <?php echo $page_links; ?>
                    </div>
                <?php endif; ?>
                <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
                    <?php
                    $args['walker'] = $walker;

                    // if we're dealing with pages, let's put a checkbox for the front page at the top of the list
                    if ( 'page' == $post_type_name ) {
                        $front_page = 'page' == get_option('show_on_front') ? (int) get_option( 'page_on_front' ) : 0;
                        if ( ! empty( $front_page ) ) {
                            $front_page_obj = get_post( $front_page );
                            $front_page_obj->front_or_home = true;
                            array_unshift( $posts, $front_page_obj );
                        } else {
                            $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
                            array_unshift( $posts, (object) array(
                                'front_or_home' => true,
                                'ID' => 0,
                                'object_id' => $_nav_menu_placeholder,
                                'post_content' => '',
                                'post_excerpt' => '',
                                'post_parent' => '',
                                'post_title' => _x('Home', 'nav menu home label'),
                                'post_type' => 'nav_menu_item',
                                'type' => 'custom',
                                'url' => home_url('/'),
                            ) );
                        }
                    }

                    $posts = apply_filters( 'nav_menu_items_'.$post_type_name, $posts, $args, $post_type );
                    $checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args );

                    if ( 'all' == $current_tab && ! empty( $_REQUEST['selectall'] ) ) {
                        $checkbox_items = preg_replace('/(type=(.)checkbox(\2))/', '$1 checked=$2checked$2', $checkbox_items);

                    }

                    echo $checkbox_items;
                    ?>
                </ul>
                <?php if ( ! empty( $page_links ) ) : ?>
                    <div class="add-menu-item-pagelinks">
                        <?php echo $page_links; ?>
                    </div>
                <?php endif; ?>
            </div><!-- /.tabs-panel -->

            <p class="button-controls">
                <span class="list-controls">
                    <a href="<?php
                        echo esc_url(add_query_arg(
                            array(
                                $post_type_name . '-tab' => 'all',
                                'selectall' => 1,
                            ),
                            remove_query_arg($removed_args)
                        ));
                    ?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
                </span>

                <span class="add-to-menu">
                    <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-post-type-menu-item" id="submit-posttype-<?php echo $post_type_name; ?>" />
                    <span class="spinner"></span>
                </span>
            </p>

        </div><!-- /.posttypediv -->
        <?php
    }
}

Open in new window


Thanks!
0
Comment
Question by:martinbros
  • 4
  • 4
  • 2
10 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39223586
There are two pages (not cpt's or posts, but pages) I would like to exclude from this to hide them from my clients who will be accessing the Menus screen.  I tried adding

Where exactly in the above code are you querying for pages?
0
 
LVL 15

Expert Comment

by:eemit
ID: 39224146
Use Plugin WP Hide Pages to Hide selected pages from menus in front end.
0
 

Author Comment

by:martinbros
ID: 39224189
@eemit - WP Hide pages doesn't hide them from the metabox in the nav menus screen, which is what I need to do.

@jason1178 - I didn't write the script, but looking through I don't see where wp_list_pages is even being called.  Apparently it doesnt have to be because all of my pages are still being populated in the box.  

Anyways, any ideas how one could exclude a few pages by their id's from showing up in the metabox?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39224207
Apparently it doesnt have to be because all of my pages are still being populated in the box.  

I don't doubt it but unless I figure out how the heck it is happening, I don't have much of a prayer in changing it :)

Can you show me an edited screen-grab of the meta box with pages showing?  I might be able to reverse engineer it from guessing at the HTML.
0
 

Author Comment

by:martinbros
ID: 39224226
@jason - lol, I hear ya.  Thanks for your help.  I attached a screenshot.  It's got a mixture of cpt's and pages in the metabox.  "Website Solution", Sidebar and Contact us are all pages.  The rest in that pic are cpt's. Also, the they are listed in descending order of page id. Let me know if you need anything else.
showpages.png
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39224673
What happens if you add the exclusion parameter between lines 52 and 60?

$args = array(
            'offset' => $offset,
            'order' => 'ASC',
            'orderby' => 'title',
            'posts_per_page' => $per_page,
            'post_type' => $post_types_list,
            'suppress_filters' => true,
            'update_post_term_cache' => false,
            'update_post_meta_cache' => false
        );

Open in new window

0
 

Author Comment

by:martinbros
ID: 39225607
I added 'exclude'      => '39', and nothing happened.  The pages still show up.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39227988
And try changing suppress_filters to false in the same array...
0
 
LVL 15

Accepted Solution

by:
eemit earned 500 total points
ID: 39228572
This should work:
'post__not_in' => array(39,967)
Add this line below the line 60 in your code as here:

$args = array(
            'offset' => $offset,
            'order' => 'ASC',
            'orderby' => 'title',
            'posts_per_page' => $per_page,
            'post_type' => $post_types_list,
            'suppress_filters' => true,
            'update_post_term_cache' => false,
            'update_post_meta_cache' => false,
            'post__not_in' => array(39,967)
        );

Open in new window

0
 

Author Closing Comment

by:martinbros
ID: 39228765
Worked perfectly!  Thanks for you help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP code to retrieve NDFD weather forecasts. 6 37
Phone Dialer 5 37
Datepicker in PHP 9 21
How to fetch your row in php 14 13
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now