jaredcobb / ccb-core

Church Community Builder Core API - A WordPress plugin to sync your church data
http://www.wpccb.com
GNU General Public License v2.0
8 stars 5 forks source link

Mapping to an existing post type #12

Closed jordesign closed 6 years ago

jordesign commented 6 years ago

Hey Jared - I'm sorry to do this as an 'issue' but it seemed the best way I could find to ask a question... I'm keen to map the CCB Event data to an existing post type

I have a hunch I can do it by filtering with 'ccb_core_synchronizer_post_api_map' but I'm not certain how?

jaredcobb commented 6 years ago

Hi @jordesign,

You're correct, the ccb_core_synchronizer_post_api_map is how you'd map the events to your own custom post type. I've been meaning to write up a how-to guide in the wiki, but until then here's an example:

function ccb_core_customize_post_api_map( $post_type_maps ) {
    $post_type_maps['custom_event'] = [
        'service' => 'public_calendar_listing',
        'data' => [
            'date_start' => date( 'Y-m-d', strtotime( '1 weeks ago' ) ),
            'date_end' => date( 'Y-m-d', strtotime( '+8 weeks' ) ),
        ],
        'nodes' => [ 'items', 'item' ],
        'fields' => [
            'event_name' => 'post_title',
            'event_description' => 'post_content',
            'date' => 'post_meta',
            'start_time' => 'post_meta',
            'end_time' => 'post_meta',
            'event_duration' => 'post_meta',
            'location' => 'post_meta',
        ],
    ];
    return $post_type_maps;
}
add_filter( 'ccb_core_synchronizer_post_api_map', 'ccb_core_customize_post_api_map' );

In this case custom_event would be the slug of your existing custom post type. If you need to also synchronize any custom taxonomies (i.e. group names or event types), you can use the ccb_core_synchronizer_taxonomy_api_map filter in a similar way.

jordesign commented 6 years ago

Oh that's so awesome - thanks Jared. My one remaining question (therefore) is in terms of mapping the fields to an existing meta field... particularly the start_date and end_date

instead of 'date' => 'post_meta', is there a way i can assign it to a specific post_meta field?

'date' => 'post_meta('my_date_field')', or something?

jaredcobb commented 6 years ago

Yep, in that case there's an additional filter you can use. It's called ccb_core_synchronizer_insert_post_args and it essentially filters the $postarr array that wp_insert_post uses.

Here's a complete example:

function ccb_core_customize_post_api_map( $post_type_maps ) {
    $post_type_maps['custom_event'] = [
        'service' => 'public_calendar_listing',
        'data' => [
            'date_start' => date( 'Y-m-d', strtotime( '1 weeks ago' ) ),
            'date_end' => date( 'Y-m-d', strtotime( '+8 weeks' ) ),
        ],
        'nodes' => [ 'items', 'item' ],
        'fields' => [
            'event_name' => 'post_title',
            'event_description' => 'post_content',
        ],
    ];
    return $post_type_maps;
}
add_filter( 'ccb_core_synchronizer_post_api_map', 'ccb_core_customize_post_api_map' );

function ccb_core_customize_insert_post_args( $args, $entity, $settings, $post_type ) {
    if (
        'custom_event' === $post_type
        && ! empty( $entity->date )
        && ! empty( $entity->location )
    ) {
        $args['meta_input']['my_date_field'] = (string) $entity->date;
        $args['meta_input']['my_location_field'] = (string) $entity->location;
    }
    return $args;
}
add_filter( 'ccb_core_synchronizer_insert_post_args', 'ccb_core_customize_insert_post_args', 10, 4 );
jaredcobb commented 6 years ago

@jordesign Feel free to re-open if you have any problems with those snippets.

jordesign commented 6 years ago

Thank @jaredcobb that looks so awesome - I'm going to give it a try :)