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

Check for custom field value in different post type than current one and do something

If I understood correctly, I believe this will help you to get what you’re after. I can’t verify it works, but reading it should give some guidance:

function my_plugin_verify() {

    // --------------------------------------------------------------------------
    // 1) I'm checking if the user is logged in
    if (!is_user_logged_in())
        return false; // NOT VERIFIED

    // --------------------------------------------------------------------------
    // 2) if he has published any posts (submitted forms) in post_typeB
    $user_id = get_current_user_id();

    // get all posts_typeB by $user_id
    $users_posts_typeB = get_posts(array(
                    'author' => $user_id,
                    'post_type' => 'post_typeB',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                ));

    // if no posts, NOT VERIFIED
    if (count($users_posts) < 1)
        return false; 

    // --------------------------------------------------------------------------
    // 3) is custom_field_1 in $users_posts_typeB in post type B empty or not
    $custom_field_1_values = array();

    // cycle through post_typeB posts by $user_id
    foreach ($users_posts_typeB as $apost) {

        // see if custom_field_1 is not empty in all them
        $custom_field_1 = get_post_meta($apost->ID,'custom_field_1',true);

        // if custom_field_1 empty, user is NOT VERIFIED
        if (empty($custom_field_1) || empty($custom_field_1))
            return false; 

        // if custom_field_1 has value, record it for comparison next
        else
            $custom_field_1_values[]  = $custom_field_1;
    }

    // --------------------------------------------------------------------------
    // 4) does the value match the value of custom field (2) of any posts in post type A that is currently being viewed

    // we'll presume no matches
    $matchFound = false;

    // we'll query all post_typeA, looking for a custom_field_2 with the value of all above custom_field_1 value's
    foreach ($custom_field_1_values as $custom_field_1_value) {
        $matchingCustomValue = get_posts(array(
            'post_type' => 'post_typeA',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'meta_key' => 'custom_field_2',
            'meta_value' => $custom_field_1_value,
        ));

        // if there's a match, we'll note it
        if (count($matchingCustomValue) > 0)
            $matchFound = true;
    }

    // if there's no match, user is NOT VERIFIED
    if (!$matchFound)
        return false;

    // --------------------------------------------------------------------------
    // 5) Is the mycred (plugin) balance is empty or not
    $balance = mycred_get_users_balance( $user_id, 'piq_credits' );
    if ($balance == '0')
        return false

    // --------------------------------------------------------------------------
    // they got this far, they're verified
    return true;
}

And you’d just argue the function

if ( my_plugin_verify() )
    // user meets criteria
    // display content
)

I’m wasn’t sure what “is currently being viewed” meant in (4) and how you’d monitor that.

I wasn’t sure if post_typeB would return more than one post, or if custom_field_1 could exists for a user more than one. I wrote it for multiples.

If your queries don’t have to happen real-time and aren’t constantly updating, you’ll want to look into transients to save the verification state instead of querying each time.

Related Posts:

  1. How Can I save multiple records in same meta key?
  2. updating one custom meta field only
  3. Unable to gather Image URL from Custom Post Type’s; Custom Meta Field
  4. Show posts from WP Custom Post Type selected from a field in a metabox
  5. Querying meta values within an array
  6. Display a list of posts whose meta field values are equal to the ID of the post being viewed?
  7. what is the correct way to compare dates in a WP query_posts meta_query
  8. Advanced search form with filters for custom taxonomies and custom fields
  9. Using meta_query, how can i filter by a custom field and order by another one?
  10. meta_query for keys that aren’t yet set
  11. Custom field values deleted when trashing custom post type
  12. Admin Area Custom Type Search By Meta Fields Without Title & Content
  13. How do I Paginate Search Results for Custom Post Types?
  14. WP_Query orderby custom field then post_date in one query
  15. Filtering a WP_Query meta_query by numeric values isn’t working
  16. Ordering posts having multiple post-meta date fields
  17. Querying custom post type with 2 custom fields (date-range)
  18. Custom Queries: Joining On Meta Values From Two Custom Post Types
  19. Detect meta value changes when post is updated (post_updated)
  20. Search everything (posts, pages, tags, cpt, meta)
  21. order by meta_value serialized array
  22. Can’t sort order of wp_query with 2 meta keys
  23. How to get source of custom meta image?
  24. Display two post types ordered by two custom fields
  25. Displaying Meta Box Image
  26. How loop through posts based on custom fields
  27. ACF Upload Image in repeater from front-end with custom form? – add_post_meta()
  28. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  29. Display custom post types with custom date field value (before today) & order by custom date field
  30. Displaying Metabox value (custom post type taxonomy)
  31. Filtering by Post Meta Custom Fields – Performance
  32. Create a random unique 6 digit number as custom field for custom post type
  33. query posts and custom post type with meta key
  34. Populate Custom Fields in a Custom Post Type?
  35. Returning a list of custom post types excluding those without a specific meta_value
  36. Two near-identical custom field types – one works, the other doesn’t . What can cause this?
  37. Meta Key Value in current-user-only loop
  38. How to check if user meta field is empty in conditional else statement
  39. wp_query check if integer exists in custom field’s array
  40. Get posts between custom dates
  41. How to implement a Google map store locator
  42. Posting to a Custom Post Type from front end – user generated content
  43. A better way to add a meta box to custom post types
  44. Meta Query Not Returning Output Despite Having Matching Values
  45. Meta Query Filtering not working on Custom Meta Box using Radio Buttons
  46. How do I set all of a particular post meta to a value within the custom post type I’m in?
  47. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  48. Meta Query posts not showing on ending date of custom field
  49. Admin Custom Meta Box – Pull Last 5 Posts from Custom Post Type
  50. How to Output which matched meta_keys were found from custom_type_posts?
  51. Possible to filter custom post type with multiple meta data?
  52. filter search custom field query
  53. Custom fields for custom post type
  54. Submitting Custom Post Types with custom fields from Front-end form
  55. create custom meta box with default value
  56. delete_post_meta() for whole CPT / multiple posts?
  57. How to get specific post meta by title or id
  58. WP_Query of custom post type sorted by meta_key has unexpected results
  59. copy images from custom field to another custom field
  60. Custom Query: If One Post Object Field Value Is The Same As Another
  61. Cannot obtain custom meta information
  62. How to call a post’s metadata in shortcode.php?
  63. WP_Meta_Query object with conditionals
  64. delete duplicate meta_value with same post_id
  65. Search CPT Title AND Meta
  66. Add a class to post_class if more than one post shares same meta_value_num
  67. Add a meta field to the list of results for a custom post type
  68. Empty meta-box returns publishdate if no value is set?
  69. Filter search posts by post meta?
  70. Custom fields for post or terms which don’t update on post update
  71. order the meta query results by 2 custom fields
  72. Orderby meta_key/meta_value not displaying anything, am I overlooking anything?
  73. Using OR relation in meta_query to check for a value before sorting by another
  74. How to use multiple Meta Field from CPT as Post permalink
  75. Meta query and compare “!=” not working as expected
  76. Use WP_query to match post types based on custom field values
  77. How can I add a meta[] to my custom post type and search by term with the Rest API?
  78. Creating an archive page or simple template to list all values of a custom field of specific post type listing
  79. Where is get_post_meta value located?
  80. Custom meta fields not showing up in WP_Response Object via custom endpoint
  81. meta query multiple values for the same key
  82. Sort custom post archives by a meta value from a different custom post type?
  83. Custom post types – meta_query: search lesson which starts sooner
  84. How to Disable option of meta field if that option is selected for any other post in custom post type?
  85. Meta_Query refuses to return results
  86. Sort loop by custom field from different post type
  87. Meta_query by date for Events archive
  88. Add more custom fields when creating a new custom post type
  89. Query events post type after current date and timezone
  90. How to add post reference field to a plugin?
  91. Selecting posts older than the current Unix epoch timestamp
  92. Advanced search form with filters for custom taxonomies and custom fields
  93. loop through custom post-type with two meta_keys
  94. Custom Post Type + Custom Meta Query Not Showing 2012 Posts
  95. Get Posts ordered by a date custom meta field
  96. Function to Compare CPT Post Custom Value with Default Posts CF Value
  97. WP Query Args – search by meta_key or title
  98. WP_Query not using relation key as expected and not producing any results
  99. getEntityRecords/useEntityRecords: How to use CPT metadata?
  100. Query custom fields with three dates – start and end does not work
Categories custom-post-types Tags custom-field, custom-post-types, meta-query, meta-value, post-meta
Problems with defining UPLOADS constant
Admin panel search doesn’t work for a specific custom post type

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