Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress check other posts on publish

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #33288
    syron1988
    Member

    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.

    #82412
    syron1988
    Member

    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?

    #82369
    syron1988
    Member

    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?

    #82340
    syron1988
    Member

    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)) {

    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');
    #82449
    TheDoc
    Member

    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.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Back End’ is closed to new topics and replies.