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 に答える