Get Meta_Value from WP database

Christian

I'm trying to do a database query that will return the meta_value of a particular row in the postmeta table based on the post ID. Then pass the variable as an integer to a jQuery variable. Here's what my code looks like:

function getListings() {
  var value3 = jQuery("#input_1_3").val();
  if (value3 === 'something')  {
    value3 = <?php echo $wpdb->get_var( "SELECT meta_value 
    FROM $wpdb->postmeta WHERE post_id = 11" ); 
    ?>;
  }
}

Right now it's returning a weird series of numbers and a ";"...The result I need should be an integer.

Any help would be greatly appreciated. I'm sure it's something obvious, but don't know what.

UPDATED:

Here is a screenshot of one of the tables I'm trying to get a meta_value from. The value I want to retrieve is "5".

PostMeta

random_user_name

I would strongly recommend using the built-in WP function get_post_meta:

NOTE: You need a "key" when doing this - what value are you hoping to return? You're going to get multiple values unless you define a key:

function getListings() {
  var value3 = jQuery("#input_1_3").val();
  if (value3 === 'something')  {
    value3 = <?php echo get_post_meta(11, 'my_key', TRUE) ?>; 
  }
}

Based on your edits, it looks like the key is pack_listings, so the code would be as follows:

function getListings() {
    var value3 = jQuery("#input_1_3").val();
    if (value3 === 'something')  {
      value3 = <?php echo get_post_meta(11, 'pack_listings', TRUE) ?>; 
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Update meta_value in wp_postmeta from an API response

CONCAT(meta_key,meta_value) into different columns from wp_postmeta table

WP REST API orderby meta_value

Accessing Meta Values from WP Database

Delete all posts from wp_posts table (and all associated tables) where a meta_value in wp_postmeta matches a certain value

Looking for query to get meta_value

WP_Query when meta_value saved as serialized array

How to update meta_value to wp_postmeta

WP Query: How to get posts from a specific author OR those with a specific meta value

WP SQL add meta_key with an empty meta_value to all users

How to copy meta_value of wp_usermeta to the same table however different meta_key?

wordpress get post_id where meta_value =

get month from date meta query in WP_Query

Compare meta_value in WP_Query that is stored in the form of serialized array

How to modify the (meta_value) in the (wp_usermeta table) with Wordpress function?

Get meta information from the database with Eloquent

Update table wp_postmeta column meta_value where meta_key column equals certain value

Get Value from Firebase Database

Spinner from database - get value

How to get value from database

TCPDF get value from database

Can not get a value from database

Get all products with specific meta_key whose meta_value is equal in WooCommerce

WP echo get_user_meta single value prints 'array'

Set LENGTH of a Meta Value in args for WP get_users()

How to replace link to specific file type in wordpress database in meta_value?

Get selected value from database table?

Get a value from database and insert into string in php

Get value from cookies in database Codeigniter PHP