Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. // LOAD DEFAULT VALUES FROM DEFAULT TOUR
  2. add_action('wp_ajax_post_loader', 'post_loader');
  3.  
  4. function post_loader() {
  5.  
  6. $post_id = $_POST["post_id"];
  7.  
  8. $args = array(
  9. 'p' => $post_id,
  10. 'numberposts'=> -1, // Fetch all posts...
  11. 'post_type'=> 'location_tour', // from the 'location_tour' CPT...
  12. );
  13.  
  14. $location = new WP_Query( $args );
  15.  
  16. if ( $location->have_posts() ) :
  17. while ( $location->have_posts() ) :
  18. $location->the_post();
  19.  
  20. $title = the_field('title');
  21. $description = the_field('description');
  22.  
  23. // You have to return data as json
  24. wp_send_json([
  25. 'title' => $title,
  26. 'description' => $description
  27. ]);
  28.  
  29. //wp_reset_postdata();
  30. endwhile;
  31. endif;
  32.  
  33. // Why do you need this inside this function?
  34. // add_action('acf/prepare_field/name=default_tour', 'post_loader');
  35.  
  36. }
  37.  
  38. jQuery(document).on( 'click', '#data_fetch', function( dohvati ){
  39. dohvati.preventDefault();
  40.  
  41. 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
  42.  
  43. jQuery.ajax({
  44. url: the_ajax_script.ajaxurl, //The URL that we set for the wordpress admin-ajax.php
  45. type: "POST",
  46. dataType: 'json',
  47. data: {
  48. action: 'post_loader', // This is the name of the php function
  49. post_id: post_id,
  50. },
  51. success: function(data){
  52. console.log(data)
  53.  
  54. jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeabc041c8a").val(data.title); //This is replacing the title field - but the variables are missing
  55. jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeab8f41c89").val(data.description); //This is replacing the description field - but the variables are missing
  56.  
  57. },
  58. error: function(error){
  59. console.log(error)
  60. },
  61. });
  62.  
  63. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement