Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom meta box value changing when creating new custom post WordPress

Hey,

I'm developing a portfolio theme for which I've created a system for featuring 4 of your projects on the homepage..

I have a custom post type for the portfolio and I've created a custom meta box with a checkbox, so you can go through your portfolio posts and check the box to feature it on the homepage.. this is all working great - my loop on the homepage looks for 4 portfolio posts and inside that loop I've used an 'if' statement to only display projects that are checked/featured..

The problem is that when I then post a new project to my portfolio post type, the last project that I featured has its checkbox unchecked. So where I had my 4 featured projects on the homepage - I post a new project and feature number 4 loses it's check value and now there's only 3 featured..

Here's my metabox code..


// Create a custom meta box to check for featured projects
add_action("admin_init", "featured_check_admin");

function featured_check_admin() {
add_meta_box("featured_check-meta", "Featured Project", "featured_check", "portfolio", "side", "high");
}

function featured_check() {
global $post;
$custom = get_post_custom($post->ID);
$featured_check = $custom["featured_check"][0];
?>
<div style="padding: 10px;">
<label style="padding: 0 5px 0 0;">Feature this project?</label>
<input type="checkbox" id="featured_check" name="featured_check" value="1" <?php if( $featured_check == 1 ) echo 'checked="yes"'; ?> />
</div>
<?php
}


And here's my homepage loop..


<div id="featured-projects" class="inner-container">

<?php $featured_projects = new WP_Query('post_type=portfolio&showposts=4'); while( $featured_projects->have_posts() ) : $featured_projects->the_post(); ?>

<?php if( get_post_meta($post->ID, 'featured_check', true) ) { ?>

<div class="feature-project">
<?php the_post_thumbnail('featured'); ?>
</div><!-- end .feature-project -->

<?php } ?>

<?php endwhile; wp_reset_postdata(); ?>

</div><!-- end #featured-projects -->


Why does publishing a new portfolio post cause one of the featured projects to lose it's checkbox value?

Cheers!

Answers (2)

2011-06-15

Lee Willis answers:

You haven't posted the code that saves your values, but it's possible that you're not checking for / excluding auto-saves , e.g.

http://techwithketan.wordpress.com/2011/04/14/auto-save-problem-for-custom-variables/

You'd use it right at the top of your save_featured_details() function, e.g.

function save_featured_details() {

global $post;

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post->ID;

update_post_meta($post->ID, "featured_check", $_POST["featured_check"]);

}


Paul Winslow comments:

Woops, here it is..


// Create a custom meta box to check for featured projects
add_action("admin_init", "featured_check_admin");

function featured_check_admin() {
add_meta_box("featured_check-meta", "Featured Project", "featured_check", "portfolio", "side", "high");
}

function featured_check() {
global $post;
$custom = get_post_custom($post->ID);
$featured_check = $custom["featured_check"][0];
?>
<div style="padding: 10px;">
<label style="padding: 0 5px 0 0;">Feature this project?</label>
<input type="checkbox" id="featured_check" name="featured_check" value="1" <?php if( $featured_check == 1 ) echo 'checked="yes"'; ?> />
</div>
<?php
}

add_action('save_post', 'save_featured_details');

function save_featured_details() {
global $post;
update_post_meta($post->ID, "featured_check", $_POST["featured_check"]);
}


Paul Winslow comments:

Could you show me where to use that autosave conditional statement in my code?

Cheers!


Lee Willis comments:

I've updated my original answer to show where the checks would go.


Paul Winslow comments:

I've added the autosave check but doesn't seem to do the job. There's now only specific portfolio posts that I can feature - others don't show up on the homepage.


Paul Winslow comments:

Okay.. if I take away 'showposts=4' from the homepage loop it seems to solve it.. I can make a new post to the portfolio post type and my 4 featured/checked projects remain intact.. but I would quite like to keep 'showposts=4' in my loop because now if you check more than 4 posts you have unwanted posts stacking up on the homepage..

Any ideas/suggestions?

Cheers!

2011-06-15

Caroline Keim answers:

Try using


<div id="featured-projects" class="inner-container">



<?php $featured_projects = new WP_Query('post_type=portfolio&showposts=4&meta_key=featured_check&meta_value=true'); while( $featured_projects->have_posts() ) : $featured_projects->the_post(); ?>




<div class="feature-project">

<?php the_post_thumbnail('featured'); ?>

</div><!-- end .feature-project -->






<?php endwhile; wp_reset_postdata(); ?>



</div><!-- end #featured-projects -->


Paul Winslow comments:

Cheers for the suggestion, but this seems to break the loop - it doesn't pull any projects out.


Caroline Keim comments:

Maybe we need to change where I had meta_value=true to meta_value=1.

If you echo the featured_check value - what is it saving as? True or 1?


Paul Winslow comments:

featured_check value returns as 1, but if I post a new portfolio item the last item I featured still loses its checkbox value.

I'd appreciate any follow-up suggestions :)


Paul Winslow comments:

Okay.. if I take away 'showposts=4' from the homepage loop it seems to solve it.. I can make a new post to the portfolio post type and my 4 featured/checked projects remain intact.. but I would quite like to keep 'showposts=4' in my loop because now if you check more than 4 posts you have unwanted posts stacking up on the homepage..

Any ideas/suggestions?

Cheers!


Caroline Keim comments:

Do you by chance have another query on the same page? I'm thinking it could be conflicting if you didn't set a reset afterwards? Sorry just trying to figure out what could be the problem.


Paul Winslow comments:

I have two loops using a variable with WP_Query and then the main blog loop using query_posts, and I'm using wp_reset_data(); at the end of my WP_Query loops if that's what you meant?

Cheers :)


Caroline Keim comments:

Try using <?php wp_reset_query(); ?> instead.

http://codex.wordpress.org/Function_Reference/wp_reset_query


Paul Winslow comments:

Still no luck.

I've also tried using an array in my loops but no luck with that, either.


Caroline Keim comments:

Here we go again...added an if statement.



<div id="featured-projects" class="inner-container">






<?php $featured_projects = "post_type=portfolio&showposts=4&meta_key=featured_check&meta_value=1"; $my_query = new WP_Query($featured_projects); ?>
<?php if ($featured_projects->have_posts()) : while ($featured_projects->have_posts()) : $featured_projects->the_post(); ?>










<div class="feature-project">



<?php the_post_thumbnail('featured'); ?>



</div><!-- end .feature-project -->













<?php endwhile; // end of one post ?>
<?php endif; //end of loop ?>
<?php wp_reset_query(); ?>








</div><!-- end #featured-projects -->