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:
use a function that collects an array of ID of pages with
get_pages()
then use that array in conjunction with
post__not_in
andpre_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:
use a function that collects an array of ID of pages with
get_pages()
then use that array in conjunction with
post__not_in
andpre_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 Answer
Reset to default 0I 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.
get_posts
orget_pages
? You mention both. – s_ha_dum Commented Aug 7, 2013 at 21:46exclude
) that can be passed both toget_posts()
andget_pages()
? Does that parameter work? If not, why not? What happens? – Chip Bennett Commented Aug 8, 2013 at 1:01get_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 functionget_pages()
: to change the result without changing the code of the plugin. – Simon Commented Aug 8, 2013 at 7:14get_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:07get_pages()
. I want to change the behavior of this plugin without hacking its core. So I figured that I could change theget_pages()
output, because it will change the output of the plugin. Doing so, I find how to filterget_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