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 starting to delve deeper into Wordpress custom fields and wanting to replicate this page in Wordpress: http://www.clubsact.com.au/corporate/index.php

So my site is not tightly structured to a parent/child relationship, I have created a custom field 'Partner_Level' and based on the partner level entered by the author the function will write out the Feature Image associated with the post and create my index page.

Though I cant even seem to get my query working within a function. If I follow the instructions on this page: http://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/ writing straight to my template, all is good. So Im assuming I don't have something configured correctly, but at a lose as to what.

Functions.php

function the_partners($cpLevel) {
// This query grabs all fields with Metadata, matches 
 $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_date DESC
 "; 

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

if ($pageposts) {
    echo "Got em";
}
}

page_corp.php

if (have_posts()) : while (have_posts()) : the_post();
<h1><?php the_title(); ?></h1>

the_partners('Gold');
endwhile; endif; 

In the page, Im simply expecting "Got em" to be printed to the screen, though Im getting a blank.

Cheers,

Phil

share|improve this question

2 Answers 2

I ended up coming back to my original approach.

Adding global $wpdb; in my function, I was able to retrieve the information that I needed to.

Everything is working now.. Hope this helps others.

share|improve this answer

my suggesting is going for a different way by using get_pages()

for example

$args = array(
         'meta_key ' => 'Partner_Level',
         'meta_value' => 'Gold' );
$pageslist = get_pages( $args );

$pageslist should have all your posts in the "gold" level.

share|improve this answer
    
Thanks for that Peter, I will check that out... I also placed global $wpdb; inside of my function and that worked. By using the get_posts() is that going to be efficient? Could I also pass a query to get the post_thumbnails for the posts? –  101phil Jul 29 '12 at 1:37
    
get_posts() is the prefered way to fetch posts. and sure, every works fine as usual in your Loop. –  peteroak Jul 29 '12 at 2:10
    
Thanks Peter, I got it working through get_posts... Though its returning all pages in the sites, not only the ones with the Partner Level of Gold. –  101phil Jul 29 '12 at 2:47
    
$args = array( 'meta_query' => array( array( 'meta_key' => 'Partner_Level', 'meta_value' => 'Gold' ), 'post_type' => 'page', 'post_status' => 'publish' )); $pages = get_pages( $args ); var_dump($pages); –  101phil Jul 29 '12 at 2:51
    
oh my bad, i didn't notice the post_type. i've updated my answer. get_pages() has different parameters, namely the meta_query. –  peteroak Jul 29 '12 at 3:08

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.