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

Get attachment by meta_key value

I think what you are after is get_post_meta() and not a custom query.

REASONS:

  • The results from custom fields are cached and extremely streamline when it cone to performance. You can read this answer I have done recently which will explain everything

  • You are trying to get the custom field attached to a post only in a single post page. This is what get_post_meta() was designed for

You can replace all your code with something like this: (Modified from the codex)

$key_1_value = get_post_meta( get_the_ID(), 'wpcf-legislacion-gratis', true ); 
// check if the custom field has a value 
if( ! empty( $key_1_value ) ) { 
    echo $key_1_value; 
} 

EDIT

If you need to get the custom field value outside the loop like in a sidebar or function, you need to replace get_the_ID() with get_queried_object_id()

$key_1_value = get_post_meta( get_queried_object_id(), 'wpcf-legislacion-gratis', true ); 
// check if the custom field has a value 
if( ! empty( $key_1_value ) ) { 
    echo $key_1_value; 
} 

Related Posts:

  1. Getting attachments by meta value
  2. Use REGEXP in WP_Query meta_query key
  3. What is an efficient way to query based on post_meta?
  4. WP_Query not working as expected for attachments and custom meta_query
  5. How can I create a WP_Query that returns posts where one meta_value
  6. WordPress altering my custom query, How to fix it?
  7. WP Meta Query for some meta (array) values
  8. How to get sum of meta_values of a meta_key in wp_query according to conditions
  9. Order posts by meta value and Date
  10. Get meta info related to current post
  11. How to do meta_query for attachments?
  12. How to make Meta Query case sensitive?
  13. Performance when getting post meta for post retrieved by meta value
  14. Fetch Record based on meta key dates
  15. Where to put meta Keys
  16. how to make members list directory through wordpress post custom meta key.
  17. Large AND OR query timing out
  18. Filter posts by comparing custom meta value against postdate
  19. Nested array issue in meta_query
  20. Query posts by searching for a string in a meta field
  21. WP_Query meta compare must include ALL array values
  22. How do i create a custom post query when the meta value is an array?
  23. wp query to use both author id and meta_query
  24. Meta query ignores multiple values of the key
  25. Trouble with serialized metadata
  26. show most viewed post
  27. Why can my filter query SOME metadata but not other metadata?
  28. Query against multiple locations within single custom post type post
  29. How to set meta_query if get_post_meta returns nested array for that key? [duplicate]
  30. Saving custom fields for WP_Query to retrieve
  31. How to show all the associated posts with specific date of data metabox?
  32. WP Meta Query at depth 2
  33. WP_Query Posts by Metadata from Option Tree
  34. Return a single custom post from multiple meta queries
  35. WP_Query by meta key not returning any posts
  36. WP_Query with 4 post meta field arguments gives 500 internal server error
  37. WP_Query Class custom field parameters
  38. meta_query returning excluded result
  39. WP_Query: attachment image in “full” size?
  40. Use have_posts() with array of post results retrieved by $wpdb->get_results
  41. WP_Query not returning correct result with meta_query parameter
  42. How can I build a query that returns all attachments of a page and it’s children pages?
  43. What’s missing in this wp_query and meta_query
  44. Sort posts using multiple custom fields and menu_order in single query?
  45. meta_query where the meta value is not the post title
  46. meta_query weird behaviour, static int will work, but not user data
  47. Query multiple post types, but different order for each
  48. How to add date_query to meta_query array
  49. Order by the first array within a meta_query
  50. Order WP_Query by meta_key priority when ‘OR’ relation used for multiple meta values
  51. Query ACF relationship field – Comparator IN – Value array
  52. Creating a query that get all posts but places meta items first
  53. get all images from the wordpress media library with link to the post they are associated with
  54. Query on meta values and post title
  55. ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)
  56. Combine query in WP_User_Query()
  57. Multiple nested meta queries
  58. How to _GET multiple value checkbox WP_Query in Custom Toxonomy / Custom Fields
  59. Custom meta_query order for Elementor based on post meta key [closed]
  60. get_posts query is taking about 40 seconds to execute
  61. What’s wrong with this meta query? (order by meta key, then title, doesn’t work)
  62. Order (by ASC) posts with meta_key so posts without values are last
  63. How can I modify the query by adding to the existing query?
  64. Custom Meta Query doesn’t work the same on two site
  65. Search filter between promo and exact price
  66. meta_query with meta values as serialize arrays
  67. ElasticPress is (aparently) messing with my search filters
  68. meta_query – check for multiple meta values in key which holds an array of values
  69. WP Query – grouping posts by same meta key, adding together values from another key
  70. Woocommerce Get Orders By Meta Value
  71. Sort by price wpdb
  72. WP_Query for a taxonomy value OR a custom post type meta field
  73. meta_query is overriding default search
  74. Multiple WP_Query args combinations according to post type
  75. WordPress Meta Query Null Values Order
  76. Multiple meta_query not returning rows
  77. WP_Query multiple value not working
  78. How to do WP_User_Query for Nested Array value Inside Nested Array field
  79. meta_query order by date present -> future then show null
  80. Searching for a specific month in a metadata saved as Timestamp (Wp_Query)
  81. Query for current post
  82. Display posts from multiple value in meta separated by comma
  83. WordPress Query custom ordering by temporary variable
  84. Custom filter from post meta
  85. Check for not NULL not working in meta query
  86. Custom query to retrieve oldest post and retrieve others with date interval
  87. Comparing Meta Field date in WPQuery using Meta_Query?
  88. Use WP_query to match post types based on custom field values
  89. Meta query orderby meta_value_num sorting by role first
  90. WP_User_Query Orderby Not Working
  91. Get unique post by meta value using wp_query
  92. Getting posts by custom field value
  93. How to create better WP_Query to look for date time which is anywhere between two meta values?
  94. How to restrict add media library only to images of the same post family?
  95. Unusual high query of user meta data
  96. how to get custom attachment url?
  97. Very slow query generated getting meta data from posts
  98. WP_Query on custom key and value
  99. WordPress Query optimaization for slow query
  100. WP_Query, ACF field and array
Categories wp-query Tags attachments, meta-query, post-meta, wp-query
WP_Query, pre_get_posts and offset
Custom post type archive page template

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