admin管理员组

文章数量:1327849

On a site this is a plugin that uses get_pages() to make a list of pages. The list is used to create a list of pages and create next page and previous page links. ( / )

The structure of pages uses some empty pages just to make a hierarchy. Those pages are not in the navigation, which is controlled by wp_nav_menu(), but they are enlisted when the plugin calls get_pages()

So I want to remove those empty pages from the request made by the plugin.

My intent is to add a custom field to each unnecessary page, and to use this custom field to select and remove them.

I did a first attempt with pre_get_posts by:

  1. use a function that collects an array of ID of pages with get_pages()

  2. then use that array in conjunction with post__not_inand pre_get_posts

But it's a deadhead since the get_pages() launched by the plugin is not the main query.

So my question is : is there a hook to the get_pages() so I can filter its results and by so, affect the behaviour of the plugin ?

On a site this is a plugin that uses get_pages() to make a list of pages. The list is used to create a list of pages and create next page and previous page links. ( http://wordpress/plugins/next-page-not-next-post/ )

The structure of pages uses some empty pages just to make a hierarchy. Those pages are not in the navigation, which is controlled by wp_nav_menu(), but they are enlisted when the plugin calls get_pages()

So I want to remove those empty pages from the request made by the plugin.

My intent is to add a custom field to each unnecessary page, and to use this custom field to select and remove them.

I did a first attempt with pre_get_posts by:

  1. use a function that collects an array of ID of pages with get_pages()

  2. then use that array in conjunction with post__not_inand pre_get_posts

But it's a deadhead since the get_pages() launched by the plugin is not the main query.

So my question is : is there a hook to the get_pages() so I can filter its results and by so, affect the behaviour of the plugin ?

Share Improve this question edited Nov 2, 2016 at 23:21 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Aug 7, 2013 at 21:33 SimonSimon 1,2181 gold badge12 silver badges22 bronze badges 9
  • 1 get_posts or get_pages? You mention both. – s_ha_dum Commented Aug 7, 2013 at 21:46
  • 2 You mention hooks, but did you try using the arguments (especially exclude) that can be passed both to get_posts() and get_pages()? Does that parameter work? If not, why not? What happens? – Chip Bennett Commented Aug 8, 2013 at 1:01
  • @s_ha_dum : sorry, typo. I need to hook get_pages(). @Chip_Bennett : I can't change the arguments without modifying the plugin, but i'd rather not to keep it updated regulary. That's why I would hook the function get_pages() : to change the result without changing the code of the plugin. – Simon Commented Aug 8, 2013 at 7:14
  • Below, in your own answer (which "doesn't work") you say "without using get_pages() so I am sorry but I don't really know what the question is or what needs to be solved. It is not clear. – s_ha_dum Commented Aug 8, 2013 at 18:07
  • Sorry. Don't know how to make it clear. The site uses a plugin. This plugin uses get_pages(). I want to change the behavior of this plugin without hacking its core. So I figured that I could change the get_pages() output, because it will change the output of the plugin. Doing so, I find how to filter get_pages(), and this is my solution below for documentation, but I also find out that the plugins uses two other functions that I cannot bypass or filter, so my attempt for this particular case is a dead-end. – Simon Commented Aug 8, 2013 at 18:12
 |  Show 4 more comments

1 Answer 1

Reset to default 0

I find the way, but it doesn"t work for my case because the plugin uses another function to link to parent pages, without using get_pages()so the result is so inconsistent. But the filter is working.

// Get all posts with the meta key 'not_in_menu'
function get_not_in_menu_posts() {

   $post_ids = wp_cache_get( 'wl_not_in_menu' );
   if ( false === $post_ids ) {

       $post_ids = array();
       $args=array(
           'post_type' => 'page',
           'meta_key' => 'not_in_menu',
                'meta_value' => '1'
       );

       $not_in_posts = get_posts( $args );
       if( $not_in_posts ) {
       $post_ids = wp_list_pluck( $not_in_posts, 'ID' );
   }

   wp_cache_set( 'wl_not_in_menu', $post_ids );
   }

   // return an array of IDs
   return $post_ids;
}

function exclude_pseudo_page( $posts ) {

   $excluded_posts = get_not_in_menu_posts();

   $shaved_posts = array();

   foreach ( $posts as $this_post ) {

       if ( ! in_array( $this_post->ID, $excluded_posts ) ) {
           $shaved_posts[] = $this_post;
       } 

   }

   return $shaved_posts;
}
if ( ! is_admin() ) {
   add_filter('get_pages', 'exclude_pseudo_page');
}

Feel free to comment.

本文标签: hooksHooking getpages()