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.

I'm facing a weird issue trying to do what I'm going to explain now.

Context

Plugins I'm using: Custom Post Type UI / Advanced Custom Fields Functionality: I have a custom post type called "vendors". Within this post type I have several custom fields made on ACF. One of these fields is Called "Promote to Homepage" which is set as a checkbox. So the idea is simple, if that checkbox is checked show that post on the homepage.

So this seemed to work fine, but when I tried to change that "Highlighted Vendor" to another one (unchecked current, checked desired to avoid conflicts I found that the post I was setting as featured wasn't showing, nothing was shown.

I started testing then trying with other vendor posts, to find that this behavior is completely random, some of them published correctly to homepage, some others not. I tried to find out a pattern to identify what might be causing it and failed. Also tried to replicate the settings of a vendor that was failing to publish within a vendor that was publishing fine, and even setting the exact same things the vendor was still publishing.

Finally I should mention that I have a page where I'm listing some kind of "directory" where all vendors are being shown. I added a simple code to check if the "promote_to_homepage" output was correct, something like "if the checkbox is checked, say hi" and found that the posts that aren't showing on homepage, pass the test on directory page.

I'm running out of ideas about what this could be, that's why I'm trying here if someone can bring me some help.

The code I'm using in the homepage is simple:

<?php query_posts('post_type=vendors');
while (have_posts()):the_post(); 
$featured = get_field('promote_to_homepage', $post -> ID, true);
if ($featured){?><!-- show the content here -->
<?php }; endwhile; ?>

That's all.

Thanks in advance to anyone that can help me on this.

Juan.-

share|improve this question
add comment

3 Answers 3

there is no third parameter for get_field, may be causing your error.

EDIT-

In your current code, if the 10 most recent posts don't contain a post with your meta key, you'll see nothing since you're only getting 10 posts and then filtering them. Any query that doesn't specify number of posts defaults to your reading settings, which you say was 10.

also, if this is not your page's primary loop, you should be using WP_Query and not query_posts. If it is your page's primary loop, you should use a pre_get_posts action hook rather than query_posts. basically, there's no reason to ever use query_posts :)

a much more efficient means of selecting your posts via WP_Query is to query specifically for the ones with the key rather than filtering them after the fact:

$args = array(
    'posts_per_page' => -1, // get all of them
    'post_type' => 'vendors',
    'meta_key' => 'promote_to_homepage'
);
$homepage_posts = new WP_Query( $args );
while( $homepage_posts->have_posts() ):
    $homepage_posts->the_post();
    // all your normal loop stuff here
    the_title();
endwhile;
share|improve this answer
    
What third parameter should I set? Not understand very well. Could you please provide me with any example? –  Jaypee Jun 8 '12 at 17:37
    
read the documentation, you only pass name and id, there is no third parameter you're passing as true –  Milo Jun 8 '12 at 17:39
    
@Jaypee - see edit. –  Milo Jun 8 '12 at 19:50
add comment

I cant see where you check if the statement is true or not. You should have something like:

<?php 
query_posts('post_type=vendors');
while ( have_posts() ):the_post(); 
  $featured = get_field( 'promote_to_homepage', $post->ID, true );

  if ( $featured == 1 ){
    echo 'Checked';
  } else {
    echo 'Not Checked';
  }

endwhile; 

?>

The if statement could either be $featured == true or $feature == 1 as i usually go with.

share|improve this answer
    
Thank you man for trying to help me, I have tried your suggested solution but I'm still getting the error, nothing shown :( –  Jaypee Jun 8 '12 at 17:21
    
On your get_field you can se $post->ID is not correct, try my updated code. And if that not work try to se if you get any results from the $featured by print_r($featured); it in the loop. –  Pontus Abrahamsson Jun 8 '12 at 17:23
    
if($featured) is the same as if($featured == 1) or true –  Milo Jun 8 '12 at 17:27
    
Thanks for your help Pontus but I just copy & pasted the code and still not working. This is the weirdest issue I have faced. –  Jaypee Jun 8 '12 at 17:30
    
Do you get anything when you print $featured ? like this print_r($featured); –  Pontus Abrahamsson Jun 8 '12 at 17:32
show 2 more comments

Ah looked at the codex and there is no true on the get_field: http://www.advancedcustomfields.com/docs/functions/get_field/

$value = get_field( 'promote_to_homepage', $post->ID );
if( $value ) {
  echo 'Checked';
}
else {
 echo 'Not Checked';
}
share|improve this answer
1  
my answer 15 minutes ago... –  Milo Jun 8 '12 at 17:38
    
Didn't see that, have soo bad connection on the train... –  Pontus Abrahamsson Jun 8 '12 at 17:40
    
Thanks SO much Pontus, also to you Milo, but I tested that before, and It's not working either. The parameter get_field('field_name', $post -> ID, true) is working correctly on the directory. –  Jaypee Jun 8 '12 at 17:43
    
Pontus!!! I found something here!!!! I was thinking that maybe post wasn't showing because it was behind the pagination, and guess what. I went to Wordpress Settings, and changed the Reading Settings to show 100 posts instead 10, I'm know getting the vendor displayed. Any explanations?!!! I don't how could I get the same result without touching the amount of posts displayed (will affect other pages if I leave like that) –  Jaypee Jun 8 '12 at 17:57
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.