Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Im hoping someone maybe able to point out my mistake. I have a function in functions.php with a simple meta_query.

If I use this query.

$args = array(
'meta_query' => array(
  array(
   'meta_key' => 'Partner_Level',
   'meta_value' => 'Gold',
   'post_type' => 'page',
   'post_status' => 'publish' 
)));
$pages = get_pages($args);


foreach ( $pages as $page ) {
$option = '<option value="' . get_page_link( $page->ID ) . '">';
            $option .= $page->post_title;
            $option .= '</option>';
            echo $option;
}

I retrieve every page in my site, not the ones matching the $args passed to get_pages();

If I simplify the $args to

$args = array(
    'meta_key' => 'Partner_Level',
    'meta_value' => 'Gold',
    'post_type' => 'page',
    'post_status' => 'publish'
    );

Then Im not getting any results at all.

So it really is an all or none, from what I can see my query looks good, but I know Im overlooking something, but what.... ? Appreciate any help with this.

I have performed a SQL statement and Im able to retrieve the expected results using..

$querydetails = "
   SELECT wposts.*
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id
   AND wpostmeta.meta_key = 'Partner_Level'
   AND wpostmeta.meta_value = 'Gold'
   AND wposts.post_status = 'publish'
   AND wposts.post_type = 'page'
   ORDER BY wposts.post_date DESC
 "; 

So my assumption is its something with get_pages() not listening to my $args.

Thanks in advance.

share|improve this question
add comment

3 Answers

I have pulled up stumps on this approach and gone back to my original function. It goes through the posts, looks for the Partner Level passed to it, and then retrieves a list of pages. I then display a styled list with the posts thumbnail as the clickable link. Will need to tweak for reach desired accessibility level, but thats for later. I appreciate your comments to this question.

For future reference:

function the_partners($cpLevel) {
global $wpdb;

 $querydetails = "
   SELECT wposts.*
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id
   AND wpostmeta.meta_key = 'Partner_Level'
   AND wpostmeta.meta_value = '$cpLevel'
   AND wposts.post_status = 'publish'
   AND wposts.post_type = 'page'

   ORDER BY wposts.post_title ASC
 "; 

$pageposts = $wpdb->get_results($querydetails, OBJECT);

echo "<ul class=\"logobox clearfix\">";
        foreach ( $pageposts as $page ) {
                $option = '<li>';
                $option .= '<a href="' . get_page_link( $page->ID ) . '">' . get_the_post_thumbnail( $page->ID ) . '</a>';
                $option .= '</li>';
                echo $option;
        }
echo "</ul>";    

}

share|improve this answer
add comment

You have to tell the comparison type as well. Try this:

$args = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'meta_query' => array(
        array(
            'key' => 'Partner_Level',
            'value' => 'Gold',
            'compare' => 'LIKE'
        )
    )
 );
share|improve this answer
    
Thanks for that, the compare didn't work either.... –  101phil Jul 30 '12 at 13:23
add comment

When you do meta_query there is no keys called meta_key or meta_value try using only key and value.

Like This:

$args = array(
   'post_type' => 'page',
   'post_status' => 'publish',
   'meta_query' => array(
      array(
         'key' => 'Partner_Level',
         'value' => 'Gold'
      )
   )
 );

Let me know if this working. You will need to use Appropriate Meta Compare too.

share|improve this answer
    
Thanks for getting back, I have tried key and meta_key both give the same result, I get a full list of all pages, not ones with the desired custom field key value combo.. –  101phil Jul 30 '12 at 12:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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