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

How do I query for posts by partial meta key?

Unfortunately you cannot perform a meta_query using a LIKE comparison on the meta_key value when using WP_Query. I’ve been down this road…

Instead you have a couple other options if you want to maintain like status relationships as post meta and not user meta and or meta in a custom table.

Option 1

  • requires no modification of your meta schema
  • uses wpdb class to perform a custom query

Example:

//when a user likes a post...
$current_user_id = get_current_user_id();
add_post_meta($current_user_id, "like_status_{$current_user_id}", 1, false);

//later in the request...
global $wpdb;

$results = $wpdb->get_results(
    "
    SELECT meta_key 
    FROM {$wpdb->prefix}postmeta 
    WHERE meta_key 
    LIKE 'like_status_%'
    ",
    ARRAY_N
);

$results = array_map(function($value){

    return (int) str_replace('like_status_', '', $value[0]);

}, $results);

array_walk($results, function($notify_user_id, $key){

    //apply to all users except the user who just liked the post
    if ( $notify_user_id !== $current_user_id ) {
        //notify logic here...           
    }

});

Note: logic could be simplified further if you wish.

Option 2

  • requires you change your meta schema
  • requires you store the user id as the meta value
  • allows you to use WP_Query along with meta_query

Option 2 requires that you change your meta key from like_status_{user_id} to something universal such as like_status or liked_by_user_id where in turn instead of storing the value of 1 against the key, you instead store the user’s id as the value.

//when a user likes a post...
$current_user_id = get_current_user_id();
add_post_meta($current_user_id, "liked_by_user_id", $current_user_id, false);

//later in the request
$args = array(
    'post_type'  => 'post', //or a post type of your choosing
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'liked_by_user_id',
            'value' => 0,
            'type' => 'numeric'
            'compare' => '>'
        )
    )
);

$query = new WP_Query($args);   

array_walk($query->posts, function($post, $key){

    $user_ids = get_post_meta($post->ID, 'liked_by_user_id');

    array_walk($user_ids, function($notify_user_id, $key){
        
        //notify all users except the user who just like the post
        if ( $notify_user_id !== $current_user_id ) {
                
            //notify logic here...
            //get user e.g. $user = get_user_by('id', $notify_user_id);
            
        }

    });

});

Related Posts:

  1. How to only display posts whose meta_value field is not empty?
  2. Can wp_query return posts meta in a single request?
  3. order by numeric value for meta value
  4. How do I query for posts by partial meta key?
  5. Use REGEXP in WP_Query meta_query key
  6. WordPress retrieving meta data for all custom post types in list view
  7. Order by optional meta key?
  8. meta_query: using BETWEEN with floats and/or casting to DECIMAL
  9. Reduce or prevent calling of update_meta_cache
  10. Is there a way to extend WP_query so Custom Post Types can have properties?
  11. What is the most efficient way of querying posts based on visits and date for current day?
  12. Getting attachments by meta value
  13. WP_Query displaying ALL posts
  14. meta_query where value is equal to given value
  15. Query Custom Meta Value with Increment
  16. WP-CLI How to generate a list of posts with corresponding meta values
  17. What is an efficient way to query based on post_meta?
  18. WP_Query not working as expected for attachments and custom meta_query
  19. How to count post meta key values for all posts in database
  20. Group posts by meta_key
  21. How should I use posts_where to change meta_value from a string to integer?
  22. How can I create a WP_Query that returns posts where one meta_value
  23. WordPress altering my custom query, How to fix it?
  24. WP Meta Query for some meta (array) values
  25. WP Query post meta value
  26. Would this post meta be better added to the post table rather than post_meta table
  27. Custom URl parameter
  28. query posts in functions.php and update a field
  29. Compile meta values from custom loop into array and then calculate sum total
  30. How to get sum of meta_values of a meta_key in wp_query according to conditions
  31. how to fire join query with post_meta
  32. Best approach to create Hot and Trending sections
  33. WP_Query, custom sort and custom filter
  34. Order posts by meta value and Date
  35. Efficient way to update multiple post meta
  36. How to make orderby ‘meta_value_num’ OPTIONAL?
  37. Query Posts depends on custom field inside repeater field using acf
  38. Query post with meta_query where date is not in future
  39. Minimising Database Queries when using Advanced Custom Fields
  40. Get meta info related to current post
  41. Select from wp_post and multiple meta_value from wp_postmeta
  42. Pull post meta with post_query?
  43. Group WP_Query by meta_key date
  44. WP_query posts closest to todays date
  45. exclude posts with a specific custom field and value
  46. Inserting serialized value into wp_postmeta using update_post_meta
  47. How to make Meta Query case sensitive?
  48. Performance when getting post meta for post retrieved by meta value
  49. Get attachment by meta_key value
  50. Display custom field meta outside loop, site wide
  51. Using hook to use DISTINCT in a wp_query
  52. Fetch Record based on meta key dates
  53. Where to put meta Keys
  54. how to make members list directory through wordpress post custom meta key.
  55. Large AND OR query timing out
  56. get_post_meta slowing down my page load (in a plugin)
  57. New WP_Query loop in admin causes problems
  58. echo a specific meta_key queried through a custom post
  59. How to increase load time of an archive/search page (WP_Query)
  60. get_post_meta in WP_Query
  61. Which is faster wpdb & get_row or wp_query & ge_post_meta?
  62. Hide posts with meta key in WP_Query
  63. Filter posts by comparing custom meta value against postdate
  64. WordPress query posts with multiple post_meta data
  65. WP_Query – Accessing MetaValue from Query Result
  66. search serialised meta_value for date value?
  67. How do I check if an article is popular this week?
  68. Woocommerce: order posts by meta key
  69. Order WP_Query results by meta key value in custom query
  70. Why can my filter query SOME metadata but not other metadata?
  71. Query against multiple locations within single custom post type post
  72. update_post_meta performance in a loop woocommerce
  73. update_post_meta performance in a loop woocommerce
  74. How to set meta_query if get_post_meta returns nested array for that key? [duplicate]
  75. I need query_posts() to order results first by a meta value and then by post ID
  76. Look for string in posts and postmeta
  77. Query postmeta based on meta_value, return array of post_id
  78. update_post_meta() not updating
  79. I can’t get post based on its postmeta value and key
  80. Saving custom fields for WP_Query to retrieve
  81. Meta key in wp_query bug?
  82. How to show all the associated posts with specific date of data metabox?
  83. WP Meta Query at depth 2
  84. WP_Query with child element
  85. Sort by meta key within same day
  86. WP_Query Posts by Metadata from Option Tree
  87. Sorting Posts with meta value not working
  88. How to get several fields from wp_query?
  89. WP Query to order posts by multiple meta fields
  90. Return a single custom post from multiple meta queries
  91. Custom Search Query – include only custom fields and title
  92. How to count post meta key values for all posts in database
  93. How to query post ids liked by the Author
  94. loop through custom post types with meta data
  95. query by meta value then date and not empty meta value
  96. wordpress sorting using array merge by price in ascending order but price with 0 must be show last
  97. Query posts by meta value and sort by another meta key
  98. WP_Query by meta key not returning any posts
  99. Extend search query to search meta keys values based on search string
  100. How to get posts that have certain meta key value and order based on another meta key’s value
Categories wp-query Tags post-meta, wp-query
Select only post id and meta value with WP_Query
Dynamic content in 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