Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Link image with data from the wpdb

Do you need to query only the items that has banner_link value set to https://www.mypage.com ?

If no, you can remove the meta_query part from your query.

<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
    'post_type' => 'banners',
    'meta_query' => array(
      array(
        'key' => 'banner_link',
        'value' => 'https://www.mypage.com'
      )
    )
));

// now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) { ?>
        <?php while ( $get_banner->have_posts() ) : $get_banner->the_post(); ?>

          <div class="container" align="center"><a href="https://wordpress.stackexchange.com/questions/260854/<?php echo get_post_meta( $post->ID,"banner_link', true ); ?>"><?php the_post_thumbnail(); ?></a></div>

        <?php endwhile; ?>
<?php }
  wp_reset_postdata();
}
?>

UPDATE

As you’re using it inside a plugin. You can do like the following:

<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
    'post_type' => 'banners',
    'meta_query' => array(
      array(
        'key' => 'banner_link',
        'value' => 'https://www.mypage.com'
      )
    )
));

// now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) : while ( $get_banner->have_posts() ) : $get_banner->the_post();

  $output="<div class="container" align="center"><a href="".get_post_meta( $post->ID, 'banner_link', true ).'">'.get_the_post_thumbnail().'</a></div>';

endwhile;
endif;
wp_reset_postdata();
return $output;
}
?>

And then call it anywhere with <?php echo wptutsplus_home_page_banner(); ?>

Let me know if it works.

Related Posts:

  1. Why WordPress choose data serialization over json_encode?
  2. Dealing with Many Meta Values, 30+
  3. What is the most secure way to store post meta data in WP?
  4. How to delete outdated, wrongly sized images in _wp_attachment_metadata?
  5. Meta query interfering with orderby relevance
  6. Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
  7. How to solve slow WordPress site caused by attachment_metadata
  8. Delete all post meta except featured image
  9. Is it possible to store visitors IPs in wp_postmeta table?
  10. Multiple postmeta with same name for one post in wp_postmeta table
  11. Delete all post meta except featured image Using SQL
  12. One post carries 30 postmeta values, is this too much?
  13. delete post meta from db, even if does not exist?
  14. WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
  15. Storing data for a wordpress plugin
  16. Where does the actual data stored by add_post_meta
  17. Structure of postmeta meta_value for woocommerce product download
  18. Routine to convert custom post meta from old to new value
  19. Saving zero as meta value
  20. Uknown meta entries in wp_postmeta
  21. Does post_meta data need to be unserialzed?
  22. Custom Post Type not showing anything added by code anymore?
  23. Compare User meta_value with Product meta_value
  24. How to save EXIF metadata in WordPress database?
  25. _wp_attachment_metadata is not being added to database when PDF files uploaded
  26. “BS_” rows in postmeta table
  27. Migrating meta_key and meta_value from old theme to new one
  28. Database structure for thousands of posts
  29. How much post meta data is too much?
  30. How do i get value from wp_postmeta?
  31. Recommended Post Structure for DB Storage
  32. SQL query : Select a thumbnail for a media
  33. Importing 10 Years of Media into the WP Database
  34. Importing Data from a Non-WordPress database, into WP
  35. I have a 1300 line excel spreadsheet that needs to get into a wordpress site ASAP – Looking for ideas
  36. 2 WordPress sites with 2 databases but sharing the same users
  37. How to combine multiple wp_insert_post into one in order to gain better performance
  38. Insert Query not working in the form
  39. Two prefixes in database? Which one is valid?
  40. Enter user registration information in the database
  41. connecting to DB from custom php file
  42. Problem in using wpdb
  43. Make changes of wp-db.php file persistent against updates
  44. Create database on installation
  45. Importer fails to import every post
  46. Change the default data installed when setting up WordPress
  47. Remove “%” from strings in serialized arrays in wp_postmeta
  48. Clear Terms from Taxonomy for Specific Post IDs?
  49. Options on how to Display member specific data from a MS SQL database
  50. Moving WordPress site to new domain – database import [duplicate]
  51. Location of WP default mySQL tables creation file?
  52. Directly editing WP database issues
  53. Installation question
  54. Disable Database Update Required ? break my website
  55. Is it risky if I update all url in the database due to my site url changed?
  56. add_post_meta — not working
  57. Select From wpdb – Author/User Directory page
  58. Say I have a tech blog, how best would I store technical specs for a phone, if i use custom post types
  59. How can I query the db to access current post information?
  60. Need help fixing sql syntax error after WP 3.2 upgrade
  61. Need help with creating a searchable user data by name or specific number like ID
  62. $wpdb->insert not inserting all rows
  63. Intentionally corrupt WordPress database
  64. Best Practice? – Saving multiple Values as Serialized Data / Saving each Value per Row / Dedicated Table
  65. Insert NULL value using prepare()
  66. I need to find posts that contain YouTube links without http:// or https:// prefix and add http:// to all of them
  67. How can I find user role in Mysql?
  68. Creating a custom form with ability to query and submit to SQL
  69. Insert variables into wpdb in custom post template
  70. $wpdb->insert duplicating rows
  71. Troubles when I want to connect to another DB
  72. Can I use a wpdb object to connect to a non-WordPress Oracle database [duplicate]
  73. Free version of Relevanssi doesn’t show ACF fields
  74. Communication in between WordPress database and another (not WordPress) database
  75. Is it possible to separate databases in WordPress site – one for orders and the other for all non-orders related
  76. Featured image from custom to WordPress database
  77. update to a new template without loosing any data
  78. Do I need to be selective about where I search and replace when pointing a domain to a subfolder used for development?
  79. Delete all users meta that named: user_avatar
  80. Form display in new page
  81. Using $wpdb to insert data into a table
  82. Accessing Information From Vendor Database On The Basis Of Customer Query
  83. spambot registering without providing email or password, bypassing registration process
  84. Halting data inserts for future refresh of post?
  85. Possible to restore wordpress database from mysql56 and sqlite files?
  86. Correct permissions for SQLite database
  87. I cannot change anything in my wordpress database from within wordpress?
  88. Processing a lot of $wpdb isn’t insert all the data
  89. Issue “Error establishing a database connection” problem.
  90. Media library images “missing” after migrating them using SQL
  91. Widgets with *internal* links do not display after database migration
  92. How can I safely delete data related to wp_post table manually from the mysql database?
  93. WP Posts Not Adding Up
  94. WordPress ERD mistake?
  95. Copy site on same server?
  96. Update references to pictures on website after moving to new URL
  97. Tool to check the database in Production [closed]
  98. How to solve notorious ‘Error Establishing a Database Connection’ [closed]
  99. database search feature
  100. Importing Concrete5 content to WordPress
Categories Database Tags database, post-meta
Adding custom cart price with Ajax in wordpress
Display posts belonging to child term

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress