Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to Get Post Meta in .Js File [duplicate]

Javascript cannot directly call PHP functions…

We need JS as well as PHP

JS will send ajax request as soon as document is ready, you need to specify postid yourself..

jQuery(document).ready(function(){
    jQuery.ajax({
        url : "/wp-admin/admin-ajax.php",
        type: "POST",
        data: {'action': 'PriceAJAX', 'postid': 12345},
        success: function(response) {
            Price = response['Theme'];
    });
});

Once it is done, the PHP will kick in, and find the post meta for your specified ID…

function PriceAJAX(){
    $ID = $_POST['postid']
    $Price = get_post_meta($ID, 'product_price', true);
    
    $Data  = array('Price' => $Price);
    wp_send_json($Data);
    wp_die();
}
add_action( 'wp_ajax_nopriv_PriceAJAX', 'PriceAJAX');
add_action( 'wp_ajax_PriceAJAX', 'PriceAJAX' );

Related Posts:

  1. Issue migrating a checkbox-type meta field to the block editor
  2. Custom Media Meta fields to alter the generated image HTML that is inserted into a post for my animated GIF image player?
  3. SyntaxError: Unexpected token o in JSON at position 1
  4. What does the function then() mean in JavaScript?
  5. Understanding unique keys for array children in React.js
  6. (Google Map API) Geocode was not successful for the following reason: REQUEST_DENIED
  7. Generate random number between two numbers in JavaScript
  8. Difference between npx and npm?
  9. How do I copy to the clipboard in JavaScript?
  10. Array of JSON Objects
  11. Cannot read property ‘push’ of undefined when combining arrays
  12. Can’t resolve module (not found) in React.js
  13. Is there a difference between /\s/g and /\s+/g?
  14. How to read an external local JSON file in JavaScript?
  15. How to call an async arrow function with await
  16. Get the current URL with JavaScript?
  17. How do I chop/slice/trim off last character in string using Javascript?
  18. What is causing this error – “Fatal error: Unable to find local grunt”
  19. Escape quotes in JavaScript
  20. “Uncaught (in promise) undefined” error when using with=location in Facebook Graph API query
  21. Firebase Permission Denied
  22. Using Javascript in CSS
  23. How can I mock an ES6 module import using Jest?
  24. JavaScript variable assignments from tuples
  25. jQuery $.cookie is not a function
  26. Setting background-image using jQuery CSS property
  27. What is ESNext?
  28. SyntaxError: Cannot use import statement outside a module
  29. Appending to an object
  30. What is the meaning of “$” sign in JavaScript
  31. How to determine the installed webpack version
  32. Error: Failed to execute ‘appendChild’ on ‘Node’: parameter 1 is not of type ‘Node’
  33. How to validate date with format “mm/dd/yyyy” in JavaScript?
  34. How can I group data with an Angular filter?
  35. jQuery disable/enable submit button
  36. Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop
  37. node-request – Getting error “SSL23_GET_SERVER_HELLO:unknown protocol”
  38. React.js – Can’t read property of undefined
  39. SyntaxError: import declarations may only appear at top level of a module
  40. Jest TypeError: Path must be a string. Received undefined
  41. JQuery issue “TypeError: $.getJSON is not a function”
  42. Why is marquee deprecated and what is the best alternative?
  43. For loop for HTMLCollection elements
  44. Refresh/reload the content in Div using jquery/ajax
  45. How to take keyboard input in JavaScript?
  46. How to access Session variables and set them in javascript?
  47. Google Maps JS API v3 – Simple Multiple Marker Example
  48. HTML onchange (this.value)
  49. SyntaxError: Use of const in strict mode
  50. d3 is not defined – ReferenceError
  51. How can I get form data with JavaScript/jQuery?
  52. How do I load an HTML page in a div using JavaScript?
  53. Check time difference in Javascript
  54. How to determine if Javascript array contains an object with an attribute that equals a given value?
  55. Download a file by jQuery.Ajax
  56. What does “export default” do in JSX?
  57. Strip all non-numeric characters from string in JavaScript
  58. Javascript – Track mouse position
  59. Adding javascript to child theme
  60. What does l10n.js do in WordPress 3.1? And how do I remove it?
  61. Open media frame and select an attachment
  62. Loading different JS for http and https
  63. How to get the ToggleControl Gutenberg component working for a PHP Block
  64. How to append a URL parameter to all outbound URLs
  65. Loading Javascript Only When Specific Content Existing in a Post?
  66. How to pre-fill Google Forms URLs with logged-in WordPress user’s data
  67. Use of Javascript with Dojo/Dijit on a WordPress page
  68. Javascript Helpers
  69. wp_register_script unable to link bootstrap JS
  70. Javascript that will execute on only child pages of a specific parent
  71. How do I change number of columns in a proper way in twentyfourteen child theme?
  72. How do I embed a Javascript form directly into a page?
  73. Using google graph api with wordpress
  74. Using wp.data.select get actual tags (not id’s) used in post
  75. Enqueue script if screen is greater than 500
  76. JavaScript && operator in visual editor
  77. How to deal with the confliction between PHP-Markdown and MathJax?
  78. Programatically update posts in database from one block to another using transform
  79. Mouse wheel Auto scroll to anchor [closed]
  80. Dynamic Custom Fields
  81. How to load JS for specific screen size using wp_enqueue_script?
  82. I am trying to add current logged in user to my zoho chat
  83. Where to upload JavaScript file in WordPress
  84. Video script issue, JavaScript attribute remains ‘undefined’
  85. Conditional save return on Gutenberg Block
  86. Can I alter the block editor’s paste text behavior?
  87. Custom Block – save.js function not saving attributes
  88. Headless wp with react. How to handle routes?
  89. Where to paste Google Map Snippet / JavaScript / CSS for WordPress integration
  90. Issues trying to get an element by id
  91. Setting Variable Path to Template Directory inside Script
  92. Dynamically add Js
  93. How to set javascript options for select2 in PHP array of plugin
  94. Defer Parsing of “createjs.min.js” not working
  95. script not working anymore after saving widget settings
  96. how to refresh page after user logout with ‘wp_logout_url’?
  97. load-scripts.php loads incorrect file names
  98. Don’t understand how to link Javascript files in WordPress
  99. Get Comment Text via REST API
  100. Sending GravityForms to custom HTML
Categories javascript Tags javascript, post-meta
get_post_gallery with Gutenberg
Merge get_comments & get_posts into a single query

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress