Addzy
Addzy

Reputation: 674

Database Query not working, Wordpress

I've been following a tutorial by Steve Marks which goes throught adding information from posts to a new Database Table as well as the usual postmeta one. However no matter what I try, It will not store the information in the new table, can anyone see where I'm going wrong?

Tutorial is available here: http://biostall.com/performing-a-radial-search-with-wp_query-in-wordpress my post type is festival-event and i made the names of my custom post types the same as in the function to see if I was missing something there.

The SQL I used to creat the table in the db was:

CREATE TABLE IF NOT EXISTS `lat_lng_post` (  
  `post_id` bigint(20) unsigned NOT NULL,  
  `lat` float NOT NULL,  
  `lng` float NOT NULL  
) ENGINE=InnoDB;  

And the function in my functions.php file is:

function save_lat_lng( $post_id )   
{  
    global $wpdb;  

    // Check that we are editing the right post type  
    if ( 'location' != $_POST['festival-event'] )   
    {  
        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" => $_POST['lat_field_name'],  
            "lng" => $_POST['lng_field_name']  
        ),   
        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" => $_POST['lat_field_name'],  
            "lng" => $_POST['lng_field_name']  
        ),   
        array(   
            "%d",   
            "%f",  
            "%f"  
        )   
        );  
    }  

}  
add_action( 'save_post', 'save_lat_lng' );

I've been researching load on what the problem could be but no luck whatsoever, I'm really stuck. Attached is images of PHPMyAdmin so you can see the dbenter image description here

enter image description here

Upvotes: 0

Views: 106

Answers (2)

Addzy
Addzy

Reputation: 674

Although the answer from @GentlemanMax was relatively useful, the actual mistake was rookie error and by me and was solved on another site, The answer was this

if ( 'festival-event' != $_POST['post_type'] )

rather than this

if ( 'post_type' != $_POST['festival-event'] )

Upvotes: 0

TheGentleman
TheGentleman

Reputation: 2362

Without seeing the tutorial you're following it is hard to say what the issue is. However, this block strikes me as a very strange way to check the post type:

if ( 'location' != $_POST['festival-event'] )   
{  
    return;  
}  

Try changing that block to something like this:

if ( get_post_type( $post_id ) != 'festival-event'] )   
{  
    return;  
}  

Alternatively, if you are using WordPress 3.7 or above you can remove that block entirely and hook to save_post_festival-event. In that scenario, your last line would look like

add_action('save_post_festival-event', 'save_lat_lng')

EDIT: To make this work with ACF

function save_lat_lng( $post_id )   
{  


    // Check that we are editing the right post type  
    if ( get_post_type($post_id) != 'festival-event' || empty($_POST['acf'] ))   
    {  
        return;  
    }  

    global $wpdb; 
    $fields = $_POST['acf'];

    // Check if we have a lat/lng stored for this property already  
    $check_link = $wpdb->get_var($wpdb->prepare(
        "SELECT COUNT(*) FROM lat_lng_post WHERE post_id = %d", $post_id
    ));  
    if (!empty($check_link))
    {  
        // We already have a lat lng for this post. Update row  
        $wpdb->update(   
            'lat_lng_post',   
            array(   
                "lat" => $fields['lat_field_name'],  
                "lng" => $fields['lng_field_name']  
            ),   
            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" => $field['lat_field_name'],  
                "lng" => $field['lng_field_name']  
            ),   
            array(   
                "%d",   
                "%f",  
                "%f"  
            )   
        );  
    }  
}  
add_action( 'acf/save_post', 'save_lat_lng', 1 );

*Note, you will have to change lat_field_name and lng_field_name to whatever you set them to when you created the fields via ACF.

I also changed your query to make use of $wpdb->prepare

While not related to this issue, I would recommend against creating a custom table without a wordpress prefix, in your case wp_

Upvotes: 2

Related Questions