Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LOAD DEFAULT VALUES FROM DEFAULT TOUR
- add_action('wp_ajax_post_loader', 'post_loader');
- function post_loader() {
- $post_id = $_POST["post_id"];
- $args = array(
- 'p' => $post_id,
- 'numberposts'=> -1, // Fetch all posts...
- 'post_type'=> 'location_tour', // from the 'location_tour' CPT...
- );
- $location = new WP_Query( $args );
- if ( $location->have_posts() ) :
- while ( $location->have_posts() ) :
- $location->the_post();
- $title = the_field('title');
- $description = the_field('description');
- // You have to return data as json
- wp_send_json([
- 'title' => $title,
- 'description' => $description
- ]);
- //wp_reset_postdata();
- endwhile;
- endif;
- // Why do you need this inside this function?
- // add_action('acf/prepare_field/name=default_tour', 'post_loader');
- }
- jQuery(document).on( 'click', '#data_fetch', function( dohvati ){
- dohvati.preventDefault();
- var post_id = jQuery('.acf-row .selection .values ul li span').data('id'); // This takes the post ID from the selected Post(Location/Tour) in the Relationship field
- jQuery.ajax({
- url: the_ajax_script.ajaxurl, //The URL that we set for the wordpress admin-ajax.php
- type: "POST",
- dataType: 'json',
- data: {
- action: 'post_loader', // This is the name of the php function
- post_id: post_id,
- },
- success: function(data){
- console.log(data)
- jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeabc041c8a").val(data.title); //This is replacing the title field - but the variables are missing
- jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeab8f41c89").val(data.description); //This is replacing the description field - but the variables are missing
- },
- error: function(error){
- console.log(error)
- },
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement