0

我使用前端表单发布,我想要的是检查帖子是否按标题存在。如果是并且元字段值大于旧值,只需替换该值。过去有人实施过类似的事情吗?

 <?php 
 session_start();
 $user_email = $_SESSION['user_email']; 
 $user_name = $_SESSION['user_name'];
 $user_img_url = 'https://graph.facebook.com/'.$user_name.'/picture?width=200&height=200';

            global $wpdb;
            global $post;
            $title = $user_name; // get the inputted title
            $content = $_POST['content']; // get the inputted content
            $categorie = $_POST['cat'];  // get the category selected by user
            $zombies = $_POST['zombies'];
            $kliks = $_POST['klik'];
            $timess = $_POST['times'];
            $name = $_POST['namn'];


              if( 'POST' == $_SERVER['REQUEST_METHOD'] ) { // if form has been submitted


                    $my_post = array(
                     'post_title' => $title,
                     'post_content' => $content,
                     'post_status' => 'publish',
                     'post_author' => 2,
                     'post_category' => array(2),
                      );

                $my_post = wp_insert_post($my_post);
                add_post_meta($my_post, 'Zombies', $zombies);
                add_post_meta($my_post, 'klik', $kliks);
                add_post_meta($my_post, 'times', $timess);
                add_post_meta($my_post, 'namn', $name);
                add_post_meta($my_post, 'profile_photo', $user_img_url);
                wp_redirect( home_url() );


                  # if $verifica is not empty, then we don't insert the post and we display a message

              } 
        ?>
4

1 回答 1

3

您的问题对我来说不是很清楚,但是如果您想按标题查询帖子,则可以使用get_page_by_title()函数,如下所示

$post = get_page_by_title( $_POST['post_title'], OBJECT, 'post' );

要获取自定义元字段,您可以使用get_post_meta()函数,如下所示

$meta_value = get_post_meta($post->ID, 'field_name', true);

然后比较并更新您可以使用的元值,例如,

if( $_POST['custom_meta_field'] > $meta_value )
{
    // Update the meta value
    update_post_meta( $post->id, 'field_name', $meta_value );
}

update_post_meta()函数用于更新自定义元字段。

更新:(基于评论)

您可以使用以下方式获取 Facebook ID 可用的帖子

$post = get_page_by_title( $_POST['facebook_id'], OBJECT, 'post' );

此外,如果元字段是time字符串(上午 12:10),那么您必须在比较之前将其转换为时间戳/数字值,例如,

$meta_value = strtotime(get_post_meta($post->ID, 'field_name', true));

所以,它会变成类似的东西1363493400,你可以比较像

if( $_POST['custom_meta_field'] > $meta_value ){ ... }

在这种情况下,您custom_meta_field还应该是时间戳/数字值,或者您必须使用strtotime()函数对其进行转换,就像$meta_value已转换一样。

于 2013-03-16T21:13:18.870 回答