- This topic is empty.
-
AuthorPosts
-
June 26, 2011 at 3:23 pm #33288syron1988Member
Hello guys!
I’m having trouble with wordpress.
My situation is as followed. I created a custom post type, which is called backgrounds. Each background has a designer-name and of course featured image. That works like a charm.
The thingy I wanna do now is to make it possible to set a background post, as the current background. But is there any possibility to check if any other background is set to that? I mean I will create a custom field, a checkbox where it says, set as current background. When I post it, it will check which other background has it true, set it to false. Like a unique value, which only exists for one post only.Hope you guys understand me. Having some trouble with my English right now.
June 26, 2011 at 3:34 pm #82412syron1988MemberCame up with an idea. is it possible to implement a function before the publish-post action? If so, is it possible to create a query to check which post has the “as background” as true?
June 27, 2011 at 5:35 am #82369syron1988MemberMh… getting really close to it… Created an option, which will be set, whenever a post is published where the checkbox is checked by using this:
function set_background( $post_ID ) {
$option_name = 'my_backround' ;
$newvalue = $post_ID ;
$postMeta = get_post_meta( $post_ID, '_cf_background_set', true );
$newvalue = $postMeta;
if ( get_option( $option_name ) != $newvalue ) {
update_option( $option_name, $newvalue );
} else {
$deprecated = ' ';
$autoload = 'no';
add_option( $option_name, $newvalue, $deprecated, $autoload );
}
}
add_action('publish_background', 'set_background');
add_action('save_background', 'set_background');
But my problem is, that get_post_meta wont work immediately, it seems that calls the database entry too early… Like before it’s posted into database… Cause when I update the post 2 times, the correct value is inside, otherwise the value I had before is in there…
Anyone know how to get that work properly?June 27, 2011 at 7:36 am #82340syron1988MemberGot a new version ,but I cannot get the update_post_meta work properly… After setting it to current background, I want the checkbox beeing unchecked again.
Here is my code:
function set_background( $post_ID ) {
global $posts, $post;
$option_name = 'my_backround' ;
$newvalue = $post_ID ;
//$postMeta = get_post_meta( $post_ID, '_cf_background_set', true );
//$newvalue = $postMeta;
update_post_meta( $post_ID, '_cf_background_set', 0, 1 );
if (isset($_REQUEST)) {
if ( get_option( $option_name ) != $newvalue ) {
update_option( $option_name, $newvalue );
} else {
$deprecated = ' ';
$autoload = 'no';
add_option( $option_name, $newvalue, $deprecated, $autoload );
}
}
}
add_action('publish_background', 'set_background');
add_action('save_background', 'set_background');
June 27, 2011 at 12:13 pm #82449TheDocMemberSounds very interesting. I don’t have any help for you, but I thought I’d give it a little bump to see if somebody else did.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.