You’ve asked for an array and you are trying to use an object.
$key = "sc_event_year";
$value = $today_year;
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'sc_event_year' AND meta_value = $value", ARRAY_A);
foreach ($results as $result){
echo $post_id = $result['post_id']; // <-- here was the problem
}
Unless $value
is a number you could have have trouble with that query and I can’t tell if there is user-supplied data but prepare
is pretty good habit if you are going to be cooking up your own queries.
$key = "sc_event_year";
$value = $today_year;
global $wpdb;
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->postmeta} WHERE meta_key = 'sc_event_year' AND meta_value = %s",
$value
),
ARRAY_A
);
foreach ($results as $result){
echo $post_id = $result['post_id'];
}
That said, are you sure there are no Core functions or methods that will server your purpose?