Your entire approach here is wrong, so lets break it apart line by line:
$a = the_ID();
the_ID echo's the post ID, this is not the correct function to use, instead use
get_the_ID(). Notice that in WP functions beginning with
the_ output data, and functions beginning with
get_ return data.
$data = $wpdb->get_results("SELECT `id`,`guid` FROM `wp_posts` WHERE `post_type` = 'local-offer' group by `post_author`");
Here you're fetching post IDs from the post table where the post type is
local-offer, grouped by post author, but this SQL query is unnecessary and slower! You're bypassing
WP_Cache so it has to retrieve everything from scratch, and there's already an API for doing this,
$q = new WP_Query( [
'post_type' => 'local-offer',
'orderby' => 'author'
'order' => 'ASC',
'fields' => 'ids'
$data = $q->posts;
Here we use
WP_Query to fetch the posts, telling it to only fetch ID's, order by author, and only the
local-offer post type, refer to the docs for more options, as well as using it to create a post loop for displaying things.
$result = $wpdb->get_results("SELECT * FROM `wp_postmeta` WHERE `post_id` = ".$a." ");
Here we have an SQL query to fetch all key value pairs for a given post ID, however:
- there's no validation here, leaving you wide open to SQL injection attacks
- there's already an easy API call for this!
You can replace this with:
$result = get_post_meta( $a );