How do I print to the page a query of the wp_postmeta
table by filtering by meta_key
using wpdb
of wordpress
? Already with the right structure. Can anyone help me?
How do I print to the page a query of the wp_postmeta
table by filtering by meta_key
using wpdb
of wordpress
? Already with the right structure. Can anyone help me?
The wpdb
class provides a direct interface with the bank. Using an example from codex :
<?php
// set the meta_key to the appropriate custom field meta key
$meta_key = 'miles';
$allmiles = $wpdb->get_var( $wpdb->prepare(
"
SELECT sum(meta_value)
FROM $wpdb->postmeta
WHERE meta_key = %s
",
$meta_key
) );
echo "<p>Total miles is {$allmiles}</p>";
?>
Of course here you can change the meta_key
and what you want to select (in this case, sum(meta_value)
) for what works best for you.
I am particularly not adept at messing with wp global. The class documentation itself is full of warnings for use with caution. Anyway, there are many ways to interact with wordpress metadata, one of them is get_post_meta()
, but of course this varies from case to case.