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

wp query to use both author id and meta_query

The main problem is the AND logic that wordpress uses when creating a sql query.

This query will look for posts that have both author = 2 and meta query, additional_author that is LIKE 2.

Your best bet is to create your own query to achive this result.

I have created a static query for you to try

global $wpdb;

$sql="SELECT *";
$sql .= " FROM {$wpdb->prefix}posts AS p";
$sql .= " JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id";
$sql .= ' WHERE (p.post_author = 2';
$sql .= ' OR (pm.meta_key = \'additional_author\' AND pm.meta_value LIKE \'%2%\'))';
$sql .= ' AND p.post_type = \'post\'';
$sql .= ' AND p.post_status = \'publish\'';
$sql .= ' GROUP BY p.ID';
$sql .= ' ORDER BY p.post_date DESC';
$sql .= ' LIMIT 5';

$results = $wpdb->get_results($sql);

print_r($results);

This is almost an exact copy of the query WordPress has built but with a small change, OR instead of AND.

Its a bit resource pricy because OR logic will create two tables to search in but it will do the work.

We don’t have to use $wpdb->prepare because all the sql is hardcoded by us, if you would like this query to be dynamic let me know and ill update the answer.

Related Posts:

  1. Use REGEXP in WP_Query meta_query key
  2. Getting attachments by meta value
  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 make Meta Query case sensitive?
  12. Performance when getting post meta for post retrieved by meta value
  13. Get attachment by meta_key value
  14. Fetch Record based on meta key dates
  15. how to make members list directory through wordpress post custom meta key.
  16. Large AND OR query timing out
  17. Filter posts by comparing custom meta value against postdate
  18. query post by author gender
  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. Meta query ignores multiple values of the key
  24. Trouble with serialized metadata
  25. show most viewed post
  26. Why can my filter query SOME metadata but not other metadata?
  27. Query against multiple locations within single custom post type post
  28. How to set meta_query if get_post_meta returns nested array for that key? [duplicate]
  29. Saving custom fields for WP_Query to retrieve
  30. How to show all the associated posts with specific date of data metabox?
  31. WP Meta Query at depth 2
  32. WP_Query Posts by Metadata from Option Tree
  33. Return a single custom post from multiple meta queries
  34. WP_Query by meta key not returning any posts
  35. Meta_query compare operator explanation
  36. meta_query with meta values as serialize arrays
  37. How to only display posts whose meta_value field is not empty?
  38. Nested meta_query with multiple relation keys
  39. Can wp_query return posts meta in a single request?
  40. order by numeric value for meta value
  41. meta_query ‘compare’ => ‘IN’ not working
  42. How do I query for posts by partial meta key?
  43. WP Query Args – Title or Meta Value
  44. Meta query with string starting like pattern
  45. compare meta_query in get_posts arguments
  46. WordPress retrieving meta data for all custom post types in list view
  47. How to query posts based on lat-lng coordinate as post meta?
  48. how to show posts that are missing a meta_value
  49. Order by optional meta key?
  50. Set Alias for meta_query arguments in get_posts()
  51. Sorting: custom query with orderby meta_value_num THEN by title
  52. meta_query: using BETWEEN with floats and/or casting to DECIMAL
  53. Display products from specific category in shop page
  54. Reduce or prevent calling of update_meta_cache
  55. Is there a way to extend WP_query so Custom Post Types can have properties?
  56. How to Compare Two Meta Fields
  57. Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
  58. Does tax_query really beats meta_query in all situations?
  59. WP_Query with checkbox meta_query
  60. ACF Relationship Field Search Filtering [closed]
  61. What is the most efficient way of querying posts based on visits and date for current day?
  62. Add indexing to meta_value in wp_postmeta
  63. Is it possible to orderby multiple meta_keys when using meta_value_num?
  64. Custom WP_Query order by post_meta and (author) user_meta
  65. Query WooCommerce orders where meta data does not exist
  66. Query Multiple Post types each with own meta query
  67. WP_Query displaying ALL posts
  68. Execute a large WP_Query with many “AND” Meta_Queries?
  69. Query all posts where meta value is empty
  70. meta_query where value is equal to given value
  71. author.php with ACF and CPTs
  72. WP_Query min and max values
  73. Query Custom Meta Value with Increment
  74. WP-CLI How to generate a list of posts with corresponding meta values
  75. Query to return maximum of one post per author
  76. ACF datepicker meta_query Compare Dates in m/d/Y g:i a – Not in Ymd Format
  77. WP_Query using meta_query with relation OR and orderby meta_value doesn’t work
  78. Using custom meta_query with relation not working as expected
  79. Perform query with meta_value date
  80. querying with custom meta field with meta_query
  81. Order by two meta keys
  82. multiple meta key but get server load is very high
  83. Meta Query with date and time on the same Day before given time
  84. A WP_Query that will look for posts after 2 weeks ago OR with a certain meta value
  85. How to grab metabox value in wp_query meta_query key
  86. WP_Query meta_query where meta value ends in space
  87. Order by meta_value_num DESC and meta_value ASC on WP 4.0
  88. Finding all results from database within 500 miles of the given latitude and longitude [closed]
  89. Nested query inside Logical operator OR not working in meta_query
  90. Is “orderby” in WP Meta Query conflicting with Meta Query?
  91. Query by meta_key and order by meta_value_num return orderby date
  92. Show only oldest post by author
  93. Function to check if author has posted within the last x days
  94. datetime picker, timestamps and meta queries
  95. How do I create my own nested meta_query using posts_where / posts_join?
  96. How to count post meta key values for all posts in database
  97. WooCommerce: filter by parent product’s taxonomy and product variation’s meta data
  98. How to order a post type with meta_value_num and if meta_value_num does not exist then order by date
  99. Group posts by meta_key
  100. How should I use posts_where to change meta_value from a string to integer?
Categories wp-query Tags author, meta-query, post-meta, wp-query
unique url access control
Override All CSS with Custom CSS on a Page by Page Basis

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