1

I'm trying to insert data from a wordpress post into a new table database table rather than WP_postmeta. My function is working, It'll insert the $postid no problem at all. The issues I'm having is I need to inser the latitude and longitude coordinates from Marty Spellerbergs 'Address Geocoder' Plugin.

On the plugin page, Marty says you can access the cooridnates inside the loop using these:

<?php echo get_geocode_lng( $post->ID ); ?>
<?php echo get_geocode_lat( $post->ID ); ?>

Now I know being inside the functions.php file, we are not actually inside the lood, so I've tried lots of different ways of accessing this data but I just can't do it. Is there a way to edit those lines specified by Marty so they can be called in the functions?.

This is one of my many many attempts at this:

function save_lat_lng( $post_id )   
{  
    global $wpdb;  

global $post;
$custom_lat = $_POST[get_geocode_lat( $post->ID )]; 
$custom_lng = $_POST[get_geocode_lng( $post->ID )];
// Check that we are editing the right post type  
if ( 'festival-event' != $_POST['post_type'] ) 
{  
    return;  
}  

// Check if we have a lat/lng stored for this property already  
$check_link = $wpdb->get_row("SELECT * FROM lat_lng_post WHERE post_id = '" . $post_id . "'");  
if ($check_link != null)   
{  
    // We already have a lat lng for this post. Update row  
    $wpdb->update(   
    'lat_lng_post',   
    array(   
        "lat" => $custom_lat,
        "lng" => $custom_lng 
    ),   
    array( 'post_id' => $post_id ),   
    array(   
        '%f',  
        '%f'  
    )  
    );  
}  
else  
{  
    // We do not already have a lat lng for this post. Insert row  
    $wpdb->insert(   
    'lat_lng_post',   
    array(   
        'post_id' => $post_id,  
        "lat" => $custom_lat,
        "lng" => $custom_lng
    ),   
    array(   
        '%d',   
        '%f',  
        '%f'  
    )   
    );  
}  
}  
add_action( 'save_post', 'save_lat_lng' )
2
  • Try replacing $post->ID with get_the_ID().
    – Ciprian
    Commented Aug 24, 2015 at 9:57
  • Unfortunately that didn't work but thanks
    – Addzy
    Commented Aug 24, 2015 at 10:00

2 Answers 2

1

Your syntax is slightly off in a few places. I'll try and let the code explain:

function wpse_199498_meta_to_table( $post_id, $post ) {
    // Use the second argument of our hook, which is the post object
    if ( $post->post_type !== 'festival-event' )
        return;

    global $wpdb;

    // Don't need to use $_POST, just use the functions as documented
    $custom_lat = get_geocode_lat( $post_id );
    $custom_lng = get_geocode_lng( $post_id );

    $check_link = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT post_id FROM lat_lng_post WHERE post_id = %d", // Here's that missing comma!
            $post_id
        )
    );

    if ( $check_link ) {
        $wpdb->update(
            'lang_lng_post',
            array(   
                'lat' => $custom_lat,
                'lng' => $custom_lng,
            ),   
            array(
                'post_id' => $post_id,
            ),   
            array(   
                '%f',  
                '%f',
            ),
            '%d'
        );
    } else {
        $wpdb->insert(
            'lang_lng_post',
            array(   
                'post_id' => $post_id,
                'lat'     => $custom_lat,
                'lng'     => $custom_lng,
            ),   
            array(  
                '%d',
                '%f',  
                '%f',
            )
        );
    }
}

add_action(
    'wp_insert_post',            // Later than "save_post", will ensure plugin has already saved meta at this point
    'wpse_199498_meta_to_table',
    10,                          // Default priority
    2                            // Number of arguments
);
8
  • Thank you for such a detailed answer, I really appreciate it, however, it caused a fatal error on the site, and left with a white screen
    – Addzy
    Commented Aug 24, 2015 at 11:07
  • Debug, debug, debug. Set WP_DEBUG to true in your wp-config.php Commented Aug 24, 2015 at 11:08
  • I can't believe I didn't know about that, anyway, it says the issue is with this section $check_link = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM lat_lng_post WHERE post_id = %d" $post_id ) );
    – Addzy
    Commented Aug 24, 2015 at 11:17
  • Ahh, we're just missing a comma to separate the arguments - see revision. Commented Aug 24, 2015 at 11:18
  • There was also an extraneous comma further down, best to copy the whole code again. Commented Aug 24, 2015 at 11:20
0

This answer was given me to elsewhere, however thought i'd share just in case anyone has similar issues:

function save_lat_lng( $post_id )   
{  
    global $wpdb;  


global $post;
$custom_lat = get_geocode_lat( $post_id ); 
$custom_lng = get_geocode_lng( $post_id );
// Check that we are editing the right post type  
if ( 'festival-event' != $_POST['post_type'] ) 
{  
    return;  
}  


// Check if we have a lat/lng stored for this property already  
$check_link = $wpdb->get_row("SELECT * FROM lat_lng_post WHERE post_id = '" . $post_id . "'");  
if ($check_link != null)   
{  
    // We already have a lat lng for this post. Update row  
    $wpdb->update(   
    'lat_lng_post',   
    array(   
        "lat" => $custom_lat,
        "lng" => $custom_lng 
    ),   
    array( 'post_id' => $post_id ),   
    array(   
        '%f',  
        '%f'  
    )  
    );  
}  
else  
{  
    // We do not already have a lat lng for this post. Insert row  
    $wpdb->insert(   
    'lat_lng_post',   
    array(   
        'post_id' => $post_id,  
        "lat" => $custom_lat,
        "lng" => $custom_lng 
    ),   
    array(   
        '%d',   
        '%f',  
        '%f'  
    )   
    );  
}  
}  
add_action( 'save_post', 'save_lat_lng', 100 );

Also a massive thank you to @TheDeadMedic for spending so much time trying to help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.