• Resolved rmsgreig

    (@rmsgreig)


    I have created two custom post types – Seasons and Competitions what I would like to do is use data from an external api to make a wordpress plugin that will create and update these posts, I have made several attempts but so far failing to get this to work a sample of the api is:{"id":15,"startDate":"14-06-2014","endDate":"23-07-2015","competition":{"id":43,"name":"Champions League"},"sponsor":{"id":12,"name":"UEFA","description":"Uefa"}}`

    I have created two custom post types – Seasons and Competitions what I would like to do is use data from an external api to make a wordpress plugin that will create and update these posts, I have made several attempts but so far failing to get this to work a sample of the api is:

    {"id":15,"startDate":"14-06-2014","endDate":"23-07-2015","competition":{"id":43,"name":"Champions League"},"sponsor":{"id":12,"name":"UEFA","description":"Uefa"}}

    and the code so far for the plugin:

    function add_posts()
    {
    
     $season_request    = 'https://somedomain/api/info';
    
    $args = array(
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( user . ':' . password)
    )
    );
    $season_response = wp_remote_get( $season_request, $args );
     $season_data = json_decode($season_response['body']);
    
    if(! $season_data)
    return false;
    
    $query = array(
      'meta_query' => array(
    
        array(
             'key' =>'season_id',
             'value' => $season_data->id
        )
    ),
    
    'post_type' => 'seasons',
    'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
      'posts_per_page' => 1
     );
    
       $season = get_posts($query);
       $season_id = '';
       if($season)
       $season_id = $season[0] -> id;
    
       $season_post = array
      (
        'ID' => $season_id,
        'post_title' => $season_data -> startDate . endDate . 'test',
        'post_type' => 'seasons',
        'post_author' => 1,
        'post_status' => ($season) ? $season[0] -> post_status : 'publish'
    
       );
    
       $season_id = wp_insert_post($season_post);
    
      }

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

    I’m relatively new to php(not to coding) so it may be something glaringly obvious that I can’t see. I thought that this would be something that there would be some detailed documentation on but if there is it seems to be hidden from me pretty well. Any help or pointers in the right direction would be really appreciated. Thanks

    • This topic was modified 9 years, 3 months ago by rmsgreig.
    • This topic was modified 9 years, 3 months ago by bdbrown.
    • This topic was modified 9 years, 3 months ago by bdbrown.
    • This topic was modified 9 years, 3 months ago by bdbrown.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It appears you are generally on the right track. Define “WP_DEBUG” as true in wp-config.php. Then any PHP errors that occur are output to your browser so you can know exactly what needs to be fixed. If you can resolve all errors that arise, I think you will achieve your goal.

    Don’t let WP_DEBUG stay as true on a production server for any length of time as it’s a slight security risk.

    Thread Starter rmsgreig

    (@rmsgreig)

    thanks for the advice, I will give it a try and let you know how I get on

    Thread Starter rmsgreig

    (@rmsgreig)

    Just in case anyone else comes across my post while looking for help, it turns out it was a problem with the way the ssl was set up on the server, if you have the same issue add ‘sslverify’ => false in the remote get array and this should solve the issue.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Populate custom post from external api’ is closed to new replies.