0

I have 2 post types, one is for my posts and the other one is used as a submission form.

When a user is on a single post page for post type A, I am trying to do several checks to then display content -

1) I'm checking if the user is logged in

2) if he has published any posts (submitted forms) in post type B

3) If so is custom field (1) in authors published posts in post type B empty or not

4) If its not empty, does the value match the value of custom field (2) of any posts in post type A that is currently being viewed

This is what I have so far ---

<?php 
$user = wp_get_current_user();
$user_id = get_current_user_id();
$balance = mycred_get_users_balance( $user_id, 'piq_credits' );
if( (is_user_logged_in()) && (piq_user_has_posts($user->ID)) && ($balance != '0') ) { ?>

The piq_user_has_posts function is --

function piq_user_has_posts($user_id) {
  $result = new WP_Query(array(
    'author'=>$user_id,
    'post_type'=>'post_type_b',
    'post_status'=>'publish',
    'posts_per_page'=>1,
  ));
  return (count($result->posts)!=0);
}

What I need now is to be able to check - if $customfield1 = $customfield2 then do something

Again, each custom field is from a different post type and Im doing this on a single post page for one of those post types.

So far I tried the following --

$users_query = new WP_Query( array(
    'post_type' => 'piq_request',
    'meta_key' => 'piq_link',
    'meta_value' => $piq_link,
) );

//echo $piq_link;
echo $this_property_id;

if($piq_link == $this_property_id) {
0

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.

  • Hi thanks for the reply! What I meant in number 4 is that the user would be on a single post page when these checks are done. There isnt a need to search through all posts for the second custom field since it is a single post view, I only need to compare the value of the field for the post being viewed. – Rich Dec 7 '17 at 1:38
0

I have it almost working perfect. but I need to test my else statement(s) further.

I have this in my functions.php --

function piq_user_has_posts($user_id) {
  $result = new WP_Query(array(
    'author'=>$user_id,
    'post_type'=>'post_type_b',
    'post_status'=>'publish',
    'posts_per_page'=>1,
  ));
  return (count($result->posts)!=0);
}

This on my single post page for post type a --

<?php

// GET USER INFO - SOME FIELDS ARE PROBABLY NOT NEEDED 
$user = wp_get_current_user();
$user_id = get_current_user_id();

//$balance = mycred_get_users_balance( $user_id, 'piq_credits' );

// IN FUNCTIONS FILE HAS CODE FOR PIQ_USER_HAS_POSTS TO SEE IF THE USER HAS POSTS IN SPECIFIC CUSTOM POST TYPE. THEN I CHECK IF LOGGED IN
if( (piq_user_has_posts($user->ID)) && (is_user_logged_in()) ) { 

// ONCE THAT CHECKS OUT I RUN A QUERY FOR ALL THE POSTS IN POST TYPE B
$args = array(
  'numberposts'=> -1,           
  'post_type'=> 'post_type_b',
  'meta_key' => 'piq_link',
  'meta_value' => $this_property_id, // THE POST ID FOR THE POST WE ARE CURRENTLY ON IN POST TYPE A
  'author' => $user_id,
);

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) :
  while ($my_query->have_posts()) : $my_query->the_post();

// IF USER POSTS MATCH THEN I GET THE VALUES NEEDED TO RUN MY OTHER CHECKS
$user_id = get_current_user_id();
$piq_author = get_the_author_meta( 'ID' );
$piq_link = get_post_meta(get_the_id(),'piq_link',true);

// I CHECK IF ALL THE VALUES MATCH UP
if (($this_property_id == $piq_link) && ($user_id == $piq_author)) { ?>

<!-- STUFF I WANT TO SHOW IF EVERYTHING CHECKS OUT OK -->


<?php } // CLOSE IT OUT
endwhile;
endif; wp_reset_query(); // END THE LOOP ?>

<!-- IF USER HAS NO POSTS AND IS NOT LOGGED IN -->

<?php } else { ?>
NOPE
<?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.