I have a series of custom tables which hold distillery operations data on the same MySQL DB as my Wordpress. I would like to use WPDB->GetResults and then WP_INSERT_POST to take form data and create a custom post type from certain distillery table entries. I have an HTML form trying to send a request to a PHP file (code below). I notice that I cannot even see the PHP when opening the webpage. There must be something wrong with this. When debugging the form submission, I am good up until my $.ajax({ call, but I think the .php file is getting me down.
-Newbie doing his best
<!DOCTYPE html>
<html>
<body>
<p>Goofball</p>
<?php
require('header.php');
require('mydomain/test/wp-includes/wp-db.php')
if(isset($_POST['mashId'])){
$mashId = $_REQUEST['mashId'];
new_mash_post($mashId);
}
function new_mash_post($mashId) {
// Initialize the page ID to -1. This indicates no action has been taken.
$post_id = -1;
$mashes = $wpdb->get_results(
"
SELECT *
FROM mash a
INNER JOIN mashbill b ON a.mashId = b.mashId
INNER JOIN mash_ferm_junc c ON a.mashId = c.mashId
WHERE a.mashId = $mashId
"
);
$oldmashid = 0;
foreach($mashes as $mash){
if($oldmashid != 0){
$oldmashid = $mash->mashId;
$slug = $mash->mashId;
$title = $mash->mashId;
$author_id = 1;
// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title ) ) {
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'your_bottle'
)
);
$json_result = array( 'success' => true, 'post_id' => $post_id);
echo json_encode( $json_result );
// Otherwise, we'll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
echo json_encode("failed");
} // end if
} //end if mashId is new
} //end for loop over selection results
} // end programmatically_create_post
?>
</body>
</html>