treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Wordpress check other posts on publish

  • 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.
  • Came 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?
  • Mh... 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?
  • Got 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['_cf_background_set'])) {

    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');
  • Sounds 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.