0

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.org/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 ?

  • 1
    get_posts or get_pages? You mention both. – s_ha_dum Aug 7 '13 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 Aug 8 '13 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 Aug 8 '13 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 Aug 8 '13 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 Aug 8 '13 at 18:12
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.

  • "I find the way, but it doesn't work..." -- so is this or is this not a solution? – s_ha_dum Aug 8 '13 at 13:28
  • It's a solution to use a filter to exclude pages based on custom fields with get_pages(). But for my case, it does not bypass the plugin results for others reasons, so I can't use it. We could delete this thread if you think it's not worth it. – Simon Aug 8 '13 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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