-1

I have 2 custom post types created with ACF

1 is check box, meta key = google_search

2nd is a text field, meta key = search_keyword

While publishing post I want to check if the google_search is checked and there is a value in search_keyword then

  1. Get the search_keyword value and save in to a custom table
  2. if search_keyword is empty then use the Title of post and save it into the custom table.

I tried this code in functions.php and post.php (both) but no luck,

add_action('save_post', 'googleSearch');

function googleSearch()
{
global $wpdb;

if ( $_POST['google_results'] == "Y") // if checkbox (google_search) is checked
{
    if ($_POST['search_keyword'] != "") // if search_keyword has value
    {
        $sSearchKeyword = get_field('search_keyword');
    }

    else
    {
        //if search_keyword is empty then use post title as keyword.
        $sSearchKeyword = get_the_title( ); 
    }

    $wpdb->query("INSERT INTO tbl_keyword SET post_id = '$post_ID',
                                              keyword = '$sSearchKeyword'");
}
}

Any help how to get custom field values while saving post and save these values in db.

Code Poet
  • 147
  • 1
  • 3
  • 16

1 Answers1

0

If you are not using the function get_field within a wordpress loop, then you have to define the ID of the post.

get_field('search_keyword', $post_ID);

And var_dump $post_ID to confirm that this variable is actually holding the post id.

Omer Farooq
  • 3,754
  • 6
  • 31
  • 60