如何在wordpress中更新post meta上更新或保存多个复选框?

时间:2017-12-18 10:16:40

标签: php wordpress api curl wordpress-theming

我可以成功创建帖子,也可以将数据更新或插入自定义字段。

但我不知道如何更新或检查多选复选框。

以下是使用精选图片创建帖子并插入自定义字段的代码。

请建议我添加多个选择的checbox。

<?php

error_reporting(E_ALL);

@ini_set('display_errors', 1);

require('wp-load.php');
global $user_ID;

$title = $_REQUEST['title'];
$content = $_REQUEST['content'];
$status = $_REQUEST['status'];
$date = $_REQUEST['date'];
$type = $_REQUEST['type'];
$bedrooms = $_REQUEST['bedrooms'];
$bathrooms = $_REQUEST['bathrooms'];
$floor = $_REQUEST['floor'];
$sqft = $_REQUEST['sqft'];
$afterfee = $_REQUEST['afterfee'];
$sp = $_REQUEST['saleprice'];

echo $title;
echo $content;
echo $status;
echo $type;
echo $bedrooms;
echo $bathrooms;

$new_post = array(
    'post_title' => $title,
    'post_content' => $content,
    'post_status' => $status,
    'post_date' => $date,
    'post_author' => $user_ID,
    'post_type' => $type,
    'post_category' => array(0)
);

$id = wp_insert_post($new_post);

update_post_meta($id,'fave_property_bedrooms',$bedrooms);
update_post_meta($id,'fave_property_bathrooms',$bathrooms);
update_post_meta($id,'fave_video_url','www.google.com');
update_post_meta($id,'fave_property_price_postfix',$afterfee);
update_post_meta($id,'fave_property_land_postfix',$sqft);
update_post_meta($id,'fave_property_land',$floor);
update_post_meta($id,'fave_property_price_prefix','Start From');
update_post_meta($id,'fave_property_price_postfix','Per Month');
update_post_meta($id,'fave_property_price',$sp);


$post_id=$id;
// only need these if performing outside of admin environment
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');

// example image
$image = 'https://clevertechie.com/img/main/php-curl-tutorial.png';

// magic sideload image returns an HTML image, not an ID
$media = media_sideload_image($image, $post_id);

// therefore we must find it so we can set it as featured ID
if(!empty($media) && !is_wp_error($media)){
    $args = array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'post_parent' => $post_id
    );

    // reference new image to set as featured
    $attachments = get_posts($args);

    if(isset($attachments) && is_array($attachments)){
        foreach($attachments as $attachment){
            // grab source of full size images (so no 300x150 nonsense in path)
            $image = wp_get_attachment_image_src($attachment->ID, 'full');
            // determine if in the $media image we created, the string of the URL exists
            if(strpos($media, $image[0]) !== false){
                // if so, we found our image. set it as thumbnail
                set_post_thumbnail($post_id, $attachment->ID);
                // only want one image
                break;
            }
        }
    }
}
?>

0 个答案:

没有答案