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

Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]

For anyone who may have a similar need, I solved this the following way (on authors.php):

First I get the author ID:

$author = get_user_by( 'slug', get_query_var( 'author_name' ) );

// ID is accessed this way:
$author_id = $author->ID;

I then created a custom query:

        $query = "
                SELECT p.* 
                FROM   $wpdb->postmeta m 
                             JOIN $wpdb->posts p 
                                 ON p.id = m.post_id 
                WHERE  ( m.meta_key = 'medium_post_photographers' 
                                 AND m.meta_value="$author->ID" ) 
                                OR ( m.meta_key = 'medium_post_videographers' 
                                         AND m.meta_value="$author->ID" ) 
                                     AND p.post_status="publish" 
                UNION DISTINCT 
                SELECT * 
                FROM   $wpdb->posts p 
                WHERE  post_author = $author->ID 
                             AND p.post_status="publish" 
                GROUP  BY p.id 
                ORDER  BY post_date DESC 
        ";

And finally use the following to get the results:

 $author_posts = $wpdb->get_results($query, OBJECT);   

Here is a simplified version of my loop to display the results:

<?php if ( $author_posts ) : ?>
    <?php global $post; ?>
    <?php foreach ( $author_posts as $post ) : setup_postdata($post); ?>
        <h6><?php echo the_title(); ?></h6>
        ...
    <?php endforeach; ?>
<?php else: ?>
    <div class="alert">There are no posts in this category.</div>
<?php endif; ?>

I hope this helps someone!

Related Posts:

  1. Order by value in serialized custom field
  2. using multiple meta_key and meta_value in query_posts
  3. How can I sort homepage by a meta value?
  4. How do I use wp_query for WordPress search?
  5. Order query by meta_value with multiple custom fields
  6. meta_compare seems to be treating values as strings instead of integers as expected
  7. Fetch Record based on meta key dates
  8. how to make members list directory through wordpress post custom meta key.
  9. Custom WP_Query for WordPress Search Results with meta_query
  10. Unable to get specific value from post meta
  11. Get author total post votes from post meta
  12. How do i create a custom post query when the meta value is an array?
  13. Display agents (custom post type) alphabetically, except one who always shows last
  14. WP_Query – Order results by meta value
  15. Custom query with orderby meta_value of custom field
  16. what is the correct way to compare dates in a WP query_posts meta_query
  17. Can I exclude a post by meta key using pre_get_posts function?
  18. Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
  19. Can wp_query return posts meta in a single request?
  20. Custom post meta field effect on the performance on the post
  21. How to get custom post meta using REST API
  22. Difference between meta keys with _ and without _ [duplicate]
  23. Orderby meta_value only returns posts that have existing meta_key
  24. What is the index [0] for on post meta fields?
  25. What is “meta_input” parameter in wp_insert_post() used for?
  26. Query Posts or Get Posts by custom fields, possible?
  27. How to enable revisions for post meta data?
  28. The “_encloseme” Meta-Key Conundrum
  29. Best way to programmatically remove a category/term from a post
  30. Filter WP_Query for posts having a certain meta-value
  31. Using get_post_meta with new_to_publish
  32. Can I query custom meta data through WP_Query
  33. Custom field metabox not showing in back-end
  34. Meta Query with AND & OR?
  35. So much data in postmeta
  36. Can I count the number of users matching a value in a multiple value key?
  37. Getting attachments by meta value
  38. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  39. How to hide meta box values from custom fields list?
  40. Auto sort the wp-admin post list by a meta key
  41. get_post_meta() unserialize issue – returns boolean(false)
  42. What is the advantage of the wp_options design pattern?
  43. meta_query where value is equal to given value
  44. Storing meta fields multiple times OR once with multi dimensional array?
  45. Allow user to create instances of custom field
  46. Query Custom Meta Value with Increment
  47. query_posts and only show results if a custom field is not empty
  48. Display Custom Post Type Fields
  49. display specific custom fields
  50. Filter archive.php by custom meta
  51. How do I search an array stored in a custom-field using WP_Query?
  52. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  53. Meta keywords and descriptions plugin for manually editing meta for each page/post
  54. passing argument to get_template_part() or a better way to code
  55. Is it possible to store arrays in a custom field?
  56. How do I order by multiple custom fields using wp_query?
  57. Get updated meta data after save_post hook
  58. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  59. Save HTML formatted data to post meta using add_post_meta()
  60. Trying to perform complex custom field query with order by set to field value
  61. importing data from non-wordpress mysql db
  62. Query meta field using between
  63. Order Posts by Closest Numeric Values
  64. Custom query with category exclusion and post-meta “whitelist”
  65. 2 orderby in wp_query with 2 custom fields
  66. Order posts by custom field and if custom field is empty return remaining posts
  67. Can serialized arrays in DB be matched against serialized arrays with meta_query?
  68. Create meta boxes that don’t show in custom fields
  69. Transients vs CRON +Custom Fields: Caching Data Per Post
  70. Unable to save datetime custom meta field using update_post_meta() function
  71. Up/Down voting system for WordPress
  72. How to get a meta value from all post
  73. post meta data clearing on autosave
  74. Create custom field on post draft or publish?
  75. Display info from custom fields in all images’ HTML
  76. Compare meta_query decimals not working right
  77. WordPress altering my custom query, How to fix it?
  78. Ordering posts by anniversary using only day and month
  79. WP_Query multiple use of relation and/or
  80. Query Problem – Show posts within category ‘x’ that have a custom field between ‘y’ and ‘z’
  81. get_post_meta fields don’t show up on posts page
  82. Update meta values with AJAX
  83. Query on custom field count?
  84. How to break meta values into different items and avoid duplicates?
  85. copy attachments to another post type and change attachment url
  86. Cannot edit post meta fields with rest API
  87. ajax delete value from custom field array
  88. Save attachment custom fields on front end
  89. How to use pagination with get_post_meta
  90. Copying Custom Meta Values from existing post to a duplicate post
  91. Using OR in WP_Query negates the “NOT EXISTS” compare
  92. How to use query_posts() with a date filter on a custom field?
  93. Add a post meta key and value only if it does not exist on the post
  94. WP_query : meta_key with custom rule for specific value
  95. Move value of one custom field to another
  96. Order posts according to user defined order for meta values?
  97. How can I combine meta_query queries?
  98. Displaying posts with only upcoming dates according their custom field date value
  99. How to filter a dd/mm/yyyy date from a custom field in a query
  100. How to show only one post for each categories of taxonomy of custom post that contains a specific custom field
Categories custom-field Tags custom-field, post-meta, query-posts, wp-query
Add Image to the RSS plugin reader from another RSS [closed]
How to display only an excerpt of the content with custom post types?

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