• 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!!

Viewing 8 replies - 16 through 23 (of 23 total)
  • With one small change (‘meta compare’ => ‘=rather than != because I want to find the matches and then exlude them (which was my error in the first place)) this works like an absolute dream.

    Michael, you are a prince amongst men!! I can’t thank you enough!

    Thanks MichaelH, this code is pretty much what I’ve been looking for. However, I need to include pages that have a custom field value equal to the page title.

    I’ve used this code, but it doesn’t work properly – please can someone help?

    <?php
    global $post;
    $args=array(
      'post_type' => 'page',
      'meta_key' => 'Project Group',
      'meta_compare' => '!=',
      'meta_value' => '<?php the_title(); ?>'
    );
    $pages = get_posts($args);
    if ($pages) {
      $pageids = array();
      foreach ($pages as $page) {
        $pageids[]= $page->ID;
      }
     ?>
      <ul>
       <?php wp_list_pages('include='.implode(",", $pageids)); ?>
    </ul>
    <?php
    }
    ?>

    You would most likely need to change 2 lines in the args array..

    To something like..

    'meta_compare' => '=',
    'meta_value' => get_the_title()

    ..assuming you’re placing this somewhere get_the_title can find a valid post id..

    Else you might need something like this for the second line..

    'meta_value' => get_the_title( get_the_ID() )

    Interesting thread – will save me some time – here’s the alternative method I was considering. I want to set public/private pages and have the private pages show in the navigation for logged in users.

    Add a meta_key/meta_value pair to every page, with a default value of “public” set.
    Then for any private pages, set the value of the custom field to “private.”
    If a user isn’t logged in with a suitable role, call wp_list_pages with this key/value pair, else just call wp_list_pages without meta_key/meta_value parameters.

    This way you save the extra database hit you’d have with calling get_pages() to generate your exclusions.

    PS – reading the code for wp_list_pages, it seems to pass any extra arguments through to get_pages, so I would expect it to be happy with meta_compare.

    I’ll experiment and try to remember to report back.

    Just for completeness, the following code taken from the get_pages() definition in includes/post.php shows that get_pages() hardcodes the = operator for comparing custom fields and so passing meta_compare to get_pages() has no effect. Pity.

    if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) {
    		$join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
    
    		// meta_key and meta_value might be slashed
    		$meta_key = stripslashes($meta_key);
    		$meta_value = stripslashes($meta_value);
    		if ( ! empty( $meta_key ) )
    			$where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
    		if ( ! empty( $meta_value ) )
    			$where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
    
    	}

    Passing meta_key => ‘my_field_name’, meta_value => ‘success’ will always result in a MySQL WHERE clause containing $wpdb->postmeta.meta_key = my_field_name AND $wpdb->postmeta.meta_value = success

    I guess this also means that any posts/pages that you want to be evaluated must always have a custom field my_field_name assigned too.

    Yes i had mentioned that further up the thread, if you want to use meta_compare, use get_posts as i stated before…

    For this kind of thing you’d be better off writing your own query, simply so you can wrap it up in a single query, rather then several just to build an exclusion list..

    Ah, yes – thanks for reiterating that – I seem to have had a temporary blindness to the difference between get_pages & get_posts.

Viewing 8 replies - 16 through 23 (of 23 total)
  • The topic ‘exclude from wp_list_pages with meta_value’ is closed to new replies.