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. How to get all custom fields of any post type
  13. Admin Area Custom Type Search By Meta Fields Without Title & Content
  14. How do I Paginate Search Results for Custom Post Types?
  15. WP_Query orderby custom field then post_date in one query
  16. Filtering a WP_Query meta_query by numeric values isn’t working
  17. Ordering posts having multiple post-meta date fields
  18. Querying custom post type with 2 custom fields (date-range)
  19. Custom Queries: Joining On Meta Values From Two Custom Post Types
  20. Detect meta value changes when post is updated (post_updated)
  21. Search everything (posts, pages, tags, cpt, meta)
  22. I can’t set meta_key in my custom post type query
  23. order by meta_value serialized array
  24. WordPress Admin Panel search posts with custom post meta values along with title
  25. Meta Query Not Returning Output Despite Having Matching Values
  26. Meta Query Filtering not working on Custom Meta Box using Radio Buttons
  27. How do I set all of a particular post meta to a value within the custom post type I’m in?
  28. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  29. Meta Query posts not showing on ending date of custom field
  30. Admin Custom Meta Box – Pull Last 5 Posts from Custom Post Type
  31. How to Output which matched meta_keys were found from custom_type_posts?
  32. Possible to filter custom post type with multiple meta data?
  33. Importing Data from a Non-WordPress database, into WP
  34. filter search custom field query
  35. How to let users choose where to search for posts?
  36. Query based on custom fields start and end date
  37. Custom fields for custom post type
  38. Check if post with same meta value exists
  39. Submitting Custom Post Types with custom fields from Front-end form
  40. create custom meta box with default value
  41. delete_post_meta() for whole CPT / multiple posts?
  42. How to get specific post meta by title or id
  43. Getting a custom post’s custom field based on another custom post’s custom field select
  44. WP_Query of custom post type sorted by meta_key has unexpected results
  45. copy images from custom field to another custom field
  46. Custom Query: If One Post Object Field Value Is The Same As Another
  47. How to show custom field on specific custom post type posts only when filled
  48. Cannot obtain custom meta information
  49. How can I output WPAlchemy repeating fields meta values in my page template?
  50. How to call a post’s metadata in shortcode.php?
  51. WP_Meta_Query object with conditionals
  52. delete duplicate meta_value with same post_id
  53. Search CPT Title AND Meta
  54. Add a class to post_class if more than one post shares same meta_value_num
  55. Add a meta field to the list of results for a custom post type
  56. Empty meta-box returns publishdate if no value is set?
  57. Filter search posts by post meta?
  58. Custom fields for post or terms which don’t update on post update
  59. order the meta query results by 2 custom fields
  60. Orderby meta_key/meta_value not displaying anything, am I overlooking anything?
  61. Custom Post Type meta data getting deleted on bulk editing taxonomies
  62. Using OR relation in meta_query to check for a value before sorting by another
  63. How to use multiple Meta Field from CPT as Post permalink
  64. How to keep custom post type related information
  65. Query posts by current ACF meta key value on single page as related posts
  66. Meta query and compare “!=” not working as expected
  67. Orderby if between two meta fields
  68. Use WP_query to match post types based on custom field values
  69. How can I add a meta[] to my custom post type and search by term with the Rest API?
  70. Creating an archive page or simple template to list all values of a custom field of specific post type listing
  71. Where is get_post_meta value located?
  72. Custom meta fields not showing up in WP_Response Object via custom endpoint
  73. meta query multiple values for the same key
  74. Sort custom post archives by a meta value from a different custom post type?
  75. Custom post types – meta_query: search lesson which starts sooner
  76. How to Disable option of meta field if that option is selected for any other post in custom post type?
  77. Meta_Query refuses to return results
  78. Sort loop by custom field from different post type
  79. Meta_query by date for Events archive
  80. Add more custom fields when creating a new custom post type
  81. How do I extract the contents of a CPT’s custom field for all posts?
  82. Query events post type after current date and timezone
  83. Delete custom post type metadata without deleting the post in admin area
  84. How can I get the $key / $value pairs of custom fields that were added via 3rd party plugins or themes?
  85. How to add post reference field to a plugin?
  86. meta query always fails even if data is correct
  87. Selecting posts older than the current Unix epoch timestamp
  88. Unable to get the upcoming events for custom post
  89. Advanced search form with filters for custom taxonomies and custom fields
  90. WP_Query arguments: Loop through custom post type – get all entries except excluded meta_key?
  91. loop through custom post-type with two meta_keys
  92. Custom Post Type + Custom Meta Query Not Showing 2012 Posts
  93. Get Posts ordered by a date custom meta field
  94. Function to Compare CPT Post Custom Value with Default Posts CF Value
  95. WP Query Args – search by meta_key or title
  96. WP_Query not using relation key as expected and not producing any results
  97. getEntityRecords/useEntityRecords: How to use CPT metadata?
  98. Meta query with ACF relationship field
  99. SQL query based on two different custom field values
  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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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