Support

Account

Home Forums Front-end Issues Reverse query

Solved

Reverse query

  • Hello,
    I’m trying to find all posts with the user_id stored in a user relationship field called volunteers.

    If I use a meta query to search for the exact user id like so "'".$user_id."'" nothing is returned.

    If I use a meta query to search for keys that contain the user id like so $user_id it works as expected.

    This code is working

      $args = array(
        'post_type' => 'cleanup',
        'posts_per_page' => 10,
        'meta_query' => array(
          array(
            'key' => 'volunteers',
            'value' => $user_id,
            'compare' => 'LIKE'
          )
        )
      );

    while this code is not

      $args = array(
        'post_type' => 'cleanup',
        'posts_per_page' => 10,
        'meta_query' => array(
          array(
            'key' => 'volunteers',
            'value' => "'".$user_id."'",
            'compare' => 'LIKE'
          )
        )
      );

    I am using this guide to try to achieve what I’m looking for https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    I only have one user attached to these posts so it’s very strange that it’s not working as expected.

    Any idea why this could be happening?

  • Hi @tylerkd

    That’s because you were using a single quote between double quotes. Could you please do it like the following instead?

    'value' => '"'.$user_id.'"',

    I hope this helps 🙂

  • Hey James, thanks for the reply.
    I tried that just now and no luck unfortunately. Any other ideas?

  • Hi @tylerkd

    I’ve just tested it on my installation and the code is working correctly. Could you please make sure that you have set the Select multiple values? option on the field settings to “Yes”?

    If you have, could you please share the JSON or XML export file of your field group so I can test it out on my installation?

    Looking forward to your reply 🙂

  • Thanks so much for the help. It turns out the code was working properly from the beginning.. For some reason this query is only grabbing posts that belong to the logged in user. If another user created the post it doesn’t seem to count it. This is definitely related to something else I’ve done without realizing it.

    Thanks again for the awesome help!

  • I’ve discovered what’s happening. I have users adding themselves to the relationship field via an ajax button. This works perfectly and the user is added to the field and if I go to the backend to edit that post I see the user in the relationship field. Also if I loop through all the posts the user is found in the relationship field. However, when I do a reverse relationship query, the user is only counted if they are added from the back end and not from my ajax. I’m using update_field();

    Does anyone have an idea why this might be?
    Do I need to call a function to refresh the post after update_field()? or is this a bug?

  • Another interesting thing is if a user adds themselves to the relationship field with the front end button and then I go to the back end and update the post there by just pressing the update button, the user is counted in the reverse relationship query.

    My thought was that the post needs to be updated in the ajax code so I added

    
    update_field('volunteers', $volunteers, $cleanup_id);
    wp_update_post( $cleanup_id );

    $cleanup_id is passed into the ajax function as the post id. Unfortunately this doesn’t seem to update the post.

  • Hi @tylerkd

    That’s weird. Could you please check if there are any differences between the values posted from AJAX and backend in the wp_postmeta table? Also, could you please try to update the field by using the field key like this:

    update_field('field_1234567890abc', $volunteers, $cleanup_id);

    Where “field_1234567890abc” is the field key.

    Thanks 🙂

  • Hey James,
    Thanks for all the help. If I add the user via ajax this is the table
    a:8:{s:4:"type";s:4:"user";s:12:"instructions";s:0:"";s:8:"required";i:0;s:17:"conditional_logic";i:0;s:7:"wrapper";a:3:{s:5:"width";s:0:"";s:5:"class";s:0:"";s:2:"id";s:0:"";}s:4:"role";s:0:"";s:10:"allow_null";i:0;s:8:"multiple";i:1;}

    If I then press update from the admin area it stays the same but the user is counted.

    I tried it using the field key and the issue persisted.

  • This is my ajax function if it helps at all

    function add_user_to_cleanup() {
      $add_remove = $_POST['add_remove'];
      $cleanup_id = $_POST['cleanup_id'];
      $user_id = get_current_user_id();
    
      $volunteers = get_field('field_57e89bd93b402', $cleanup_id, false);
    
      if (empty($volunteers)) {
        $volunteers = array();
      }
    
      if ($add_remove == 'add' && !in_array($user_id, $volunteers)) {
        //if user is not in volunteers array then add them
          array_push($volunteers, $user_id);
      } elseif ($add_remove == 'remove') {
        //find user in array and remove them
        $pos = array_search($user_id, $volunteers);
        unset($volunteers[$pos]);
      }
    
      update_field('field_57e89bd93b402', $volunteers, $cleanup_id);
    }
    add_action( 'wp_ajax_add_user_to_cleanup', 'add_user_to_cleanup' );
    add_action( 'wp_ajax_nopriv_add_user_to_cleanup', 'add_user_to_cleanup' );
  • I removed the ajax altogether to test this another way. I used an empty form submit with a function that is hooked to init that checks posted data and runs if it finds a value.
    I also simplified the function so it only updates the field with the user id.

    function add_volunteer() {
    
      if ( isset( $_POST['add-volunteer'] ) && '1' == $_POST['add-volunteer'] ) {
    
        $cleanup_id = $_POST['cleanup_id'];
        $user_id = get_current_user_id();
    
        update_field('field_57e89bd93b402', $user_id, $cleanup_id);
    
      } 
    
    } 
    
    add_action( 'init', 'add_volunteer' );

    I also tested it with the field set to select multiple values and select single value.

    I’m still seeing the same issue though. Is it possible that this is a bug?

  • It definitely seems like a bug to me.

    I gave up and I’m doing this a different way. In my ajax function I’m adding the user to the post and additionally adding the post to a field on the user profile.

    Hope this helps someone else in the future.

  • Hi @tylerkd

    I think you copied the wrong row there. Could you please find the row with “post_id” of the cleanup ID and the “meta_key” of “volunteers” and “_volunteers” in the “wp_postmeta” table?

    But if you are happy with your current solution, then we don’t need to do anything else.

    Thanks 🙂

  • Hmm, There doesn’t seem to be a row in the postmeta table with the post id or a row with that meta_key. Maybe I should try this all out in a fresh WP install. Is there another place that row could be saved?

  • Hi @tylerkd

    All of the custom fields should be placed inside of wp_postmeta table for the posts, wp_usermeta for the users, and wp_options for the taxonomies and options page.

    I believe the fields are assigned for a custom post type, but could you please share the JSON or XML export file of your field group so I can check your setup?

    Thanks 🙂

Viewing 15 posts - 1 through 15 (of 15 total)

The topic ‘Reverse query’ is closed to new replies.