I’m having trouble getting custom field data from WordPress when using external system connections.
What’s Working vs What’s Not
External system connects to WordPress fine
Regular post content loads without issues
Custom field values won’t retrieve at all
My Setup Details
I added custom fields through a WordPress plugin. The data gets saved in the wp_postmeta database table. I can see the fields in WordPress admin but my external queries return empty results.
Code I’ve Been Testing
function fetch_meta_field_data() {
global $wp_database; // WordPress DB connection
// Query to get meta value
$query = "SELECT meta_value FROM wp_postmeta WHERE meta_id = '2847'";
// Run the query
$db_result = $wp_database->query($query);
// Store result
$field_data = '';
if ($db_result) {
$row = $db_result->fetch_object();
if ($row) {
$field_data = unserialize($row->meta_value);
}
}
return $field_data;
}
Anyone know what might be blocking the custom field retrieval? The connection works for everything else.
hey, i had a similar issue before. try using post_id in your query instead of meta_id, and make sure your meta_key is an exact match to how you saved it. WordPress can be picky, you know?
I hit the same issue with external database connections to WordPress. You’re querying by meta_id when you should be using post_id and meta_key together. Try this instead: SELECT meta_value FROM wp_postmeta WHERE post_id = [your_post_id] AND meta_key = '[your_custom_field_name]'. Also, check if your custom field plugin stores serialized data - some plugins wrap values in arrays even for simple text, so unserialize might not be needed or could be breaking things. Run the query in phpMyAdmin first to see what the actual data looks like before processing it in your code.
Your database connection looks good, but you’re handling the result object incorrectly. The query() method doesn’t provide a result set to fetch from directly - use get_results() or get_var() instead. Additionally, avoid unserializing all custom field data, as not everything requires it, and you may encounter failures when it doesn’t. First, check the actual contents of the meta_value column by executing a direct SQL query in your database tool. I’ve noticed that custom field plugins can store data differently than expected, sometimes with extra prefixes or in JSON instead of serialized PHP. Since your connection works for regular posts but fails with postmeta, it suggests an issue with your data retrieval method rather than permissions or connection problems.