如何从外部json文件创建Wordpress帖子? [英] How to create Wordpress posts from external json file?

查看:142
本文介绍了如何从外部json文件创建Wordpress帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的代码.每次刷新时,它都会创建重复的帖子.另外,如何在我的帖子中添加自定义字段?

Here is the code I am using. It creates duplicate post every time I refresh. Also, how can I add custom field to my post?

我的数组如下:

[{
    "featured":"",
    "exclusive":"",
    "promo_id":"XXX",
    "offer_id":"1",
    "title" : "Super Cars"
}]

我的php代码:

<?php 
$json = "url";
$response = file_get_contents($json);
$mydecode = json_decode($response);

for ($i = 10; $i < 15; $i++) {
    $title = str_replace("&amp;", "&", $mydecode[$i]->title);
    $id = $mydecode[$i]->offer_id;
    $link = $mydecode[$i]->link;

    if( $id === "x" ) {

        $new_post = array(
        'post_title' => $title,
        'post_content' => $description,
        'post_status' => 'draft',
        'post_author' => 1,
        'post_type' => 'coupon'
        );
        $post_id = wp_insert_post($new_post);
    }
}
?>

代码成功插入帖子,但每次刷新时重复. 如果有人可以贡献一点,那就太好了!

The code successfully inserts posts but duplicate every time I refresh. If anyone can contribute a bit, it would be great!

推荐答案

将代码更新为以下代码,

Update your code to following,

<?php
$json = "http://tools.vcommission.com/api/coupons.php?apikey=xxxxxxxxxx";
$response = file_get_contents($json);
$mydecode = json_decode($response);

for ($i = 0; $i < 15; $i++) {
    $title = str_replace("&amp;", "&", $mydecode[$i]->coupon_title);
    $description = str_replace("&amp;", "&", $mydecode[$i]->coupon_description);
    $store_name = $mydecode[$i]->offer_name;
    $coupon_type = $mydecode[$i]->coupon_type;
    $coupon_code = $mydecode[$i]->coupon_code;
    $link = $mydecode[$i]->link;
    $expiry_date = $mydecode[$i]->coupon_expiry;
    if( $coupon_type === "Coupon" ) {
        // Check if already exists
        $get_page = get_page_by_title( $title );
        if ($get_page == NULL){
            // Insert post
            $new_post = array(
                'post_title' => $title,
                'post_content' => $description,
                'post_status' => 'draft',
                'post_author' => 1,
                'post_type' => 'coupon'
            );
            // Insert post
            $post_id = wp_insert_post($new_post);
            // Insert post meta if available  
            add_post_meta( $post_id, 'meta_key', 'meta_value' );  

            // Uncomment to check if meta key is added
            // $get_meta_value = get_post_meta( $post_id, 'meta_key', true );        
            // echo "<pre>";
            // print_r($get_meta_value);
        }
    }else{
        // Update meta value
        update_post_meta($get_page->ID, 'my_key', 'meta_value');

        // Uncomment to check if meta key is added
        // $get_meta_value = get_post_meta( $get_page->ID, 'meta_key', true );        
        // echo "<pre>";
        // print_r($get_meta_value);
    }
}
?>

我希望这会有所帮助.

I hope this helps.

这篇关于如何从外部json文件创建Wordpress帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆