exclude from wp_list_pages with meta_value
-
Can we exclude pages from wp_list_pages using met_key and meta_value?
Does wordpress offer offer a way to do this?
Or do I need to build an array manually? If so, what’s the cleanest way to do this? To subtract one array (list by meta_value) from the full list?
thanks!!
-
Didn’t test this but something like this to get pages where custom field cf1 is equal to green:
<?php $args=array( 'showposts' => -1, 'post_type' => 'page', 'meta_key' => 'cf1', 'meta_compare' => '=', 'meta_value' => 'green' ); $pages = get_posts($args); if ($pages) { foreach($pages as $post) { setup_postdata($post); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php } // foreach($pages } // if ($pages ?>Can anyone think of a better method?
$list_full = wp_list_pages(‘echo=0’);
$list_exclude = wp_list_pages(‘meta_value=XYZ&meta_key=ABC&echo=0’);
$the_list = array_diff($list_full, $list_exclude);Thanks Michael. I’m looking to exclude items with meta_value green.
What does meta_compare do? Can I set that to be not equal and use it with wp_list_pages?
(I want to use wp_list_pages to utilize the class and hierarchy generated by WP)
thanks again…
So it seems like this will do the trick. I hadn’t seen meta_compare before.
<?php
$args = array(
‘meta_compare’ => ‘!=’,
‘meta_key’ => ‘Exclude_Page’,
‘meta_value’ => ‘Yes_Sir’,
);wp_list_pages( $args );
?>
wp_list_pages exclude is based on page ID and I just don’t see how you could relate that to a meta value.
http://codex.wordpress.org/Template_Tags/wp_list_pages#Exclude_Pages_from_List
Oh well, doesn’t work. I guess that meta_compare can’t be passed to wp_list_pages. that’s stinky. Back to the drawing board…
Build an array of post ids using the get_posts example then use that array with the wp_list_pages exclude argument.
You can use..
<?php wp_list_pages( array('meta_key'=>'keyname','meta_value'=>'value') ); ?>However you can’t use meta_compare when using wp_list_pages as it only supports both meta_key and meta_value, but not meta_compare..
http://codex.wordpress.org/Function_Reference/get_pagesget_postsis not documented as supporting meta_compare, but does look to support it(wp-includes/query.php – line 2146).Use get_posts if you need the meta_compare parameter, as Michael suggested.
I believe that get_posts can use almost any argument listed in the query_posts() article.
Hi,
I’m trying something similar – exclude pages which don’t have the key/value of notmobile/notmobile.
I’ve tried loads of different ways, and just used MichaelH’s code above to come up with:
<?php global $post; $args=array( 'post_type' => 'page', 'meta_key' => 'notmobile', 'meta_compare' => '!=', 'meta_value' => 'notmobile' ); $pages = get_posts($args); ?> <ul> <?php wp_list_pages('exclude'.$pages); ?> </ul>But it doesn’t exclude the pages π
Any help would be sooo great!
Thanks!
Will repeat from above:
Build an array of post ids using the get_posts example then use that array with the wp_list_pages exclude argument.
sorry to show up my limited knowledge, but I thought that’s what I did??
<?php global $post; $args=array( 'post_type' => 'page', 'meta_key' => 'notmobile', 'meta_compare' => '!=', 'meta_value' => 'notmobile' ); $pages = get_posts($args); if ($pages) { $pageids = array(); foreach ($pages as $page) { $pageids[]= $page->ID; } } ?> <ul> <?php wp_list_pages('exclude='.implode(",", $pageids)); ?> </ul>I really appreciate you coming back so quickly on this. Unfortunately I get an error:
Warning: implode() [function.implode]: Invalid arguments passed in myurl/wp-content/themes/combined/desk_index.php on line 77
Line 77 being:
<?php wp_list_pages('exclude='.implode(",", $pageids)); ?>This is beyond me to even guess…
Oops sorry, forgot to handle the situation where it finds NO pages with that meta_key/meta_compare/meta_value criteria:
<?php global $post; $args=array( 'post_type' => 'page', 'meta_key' => 'notmobile', 'meta_compare' => '!=', 'meta_value' => 'notmobile' ); $pages = get_posts($args); if ($pages) { $pageids = array(); foreach ($pages as $page) { $pageids[]= $page->ID; } ?> <ul> <?php wp_list_pages('exclude='.implode(",", $pageids)); ?> </ul> <?php } ?>
The topic ‘exclude from wp_list_pages with meta_value’ is closed to new replies.