Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got two custom post types: CD and Track. Each of them has got some metadata associated with them.

What I'm trying to do is to add Track posts to a CD post when the CD post is being edited. On the CD edit page I have "Add Track" link. When clicking on it, I would like a "New Track Form" to appear under the link that will ask for a Track (custom-post-type) information, along with all metadata that needs to be specified for a Track - as if "Add Track" link was clicked from the left WP menus. Hope that makes sense.CD

My CD and Track custom-post-types are registered as following: http://wordpress.pastebin.com/Y6aagTVs

On the edit CD page I have the following link <a class="addTrack" href="">Add Track</a>

I'm missing AJAX and the function that will handle the addition of the "New Track form". Here is what I've got so far in the AJAX function:

// Add Tracks   
$("a.addTrack").click(function () {
    opts = {
        url: ajaxurl, // ajaxurl is defined by WordPress and points to /wp-admin/admin-ajax.php
        type: 'POST',
        async: true,
        cache: false,
        dataType: 'json',
        data:{
            action: 'track_add', // Tell WordPress how to handle this ajax request
        },
        success: function(response) {
            return false;
        },
        error: function(xhr,textStatus,e) {  // This can be expanded to provide more information
            alert("There was an error adding a track.");
            return false;
        }
    };
    $.ajax(opts);
});

And the function that will handle the insertion of the "New Track Form" is

// Add Tracks
add_action('wp_ajax_track_add', 'my_ajax_admin_add_track');
function my_ajax_admin_add_track() {
}

I don't know how to go on about the AJAX and my_ajax_admin_add_track() functions. Would really appreciate any help and tips!!!

Many thanks, Dasha

share|improve this question
    
I would absolutely love to see the solution you come up with on this question. I've asked this question a number of times over the past year but still have no real working solution. You would be my hero! :) –  shawn Jun 5 '11 at 21:54
add comment

1 Answer 1

up vote 1 down vote accepted

For your my_ajax_admin_add_track() function, grab the form field data from the $_POST array, populate the required data in an array to call wp_insert_post(), then call wp_insert_post() passing it the post data. It should return the new post ID number if successful.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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