Support

Account

Home Forums Front-end Issues Multiple ID's in relationship field — LIKE query returns inappropriate posts

Solving

Multiple ID's in relationship field — LIKE query returns inappropriate posts

  • Hi there,

    I’m attempting to do a fairly simple meta query. I have a custom post type called cta, and on that post type there is a relationship field that essentially allows the user to select the page(s) for this CTA to appear.

    $args = array(
      'order' => 'DESC',
      'orderby' => 'date',
      'meta_query' => array(
        array(
          'key' => 'location',
          'value' => '"' . get_the_ID() . '""',
          'compare' => 'LIKE'
        )
      ),
      'post_type' => array('cta'),
      'posts_per_page' => 1,
      'post_status' => 'publish'
    );

    Here’s my issue. I have it set up with one single ID in there, a page with ID of 2. I since I have another page with the ID of 127, it’s showing up on _both_ page 2 and page 127. I think this is because the compare is set to LIKE (and since it’s a serialized array in the DB we can’t do anything but that).

    How do you circumvent this issue? I dug through the forum and I can’t be the only one.

  • PS.

    What I ended up doing, which is horrible and terrible for performance, is allow the query to run, and then in my loop, check *again* for the value:

    if (in_array($ID, get_field('location'))) {

    …and basically only run / output content in this if statement. This means it’s actually doing the query, retrieving the post, and then doing some junk crap to prevent it from showing up if we don’t past the in_array test.

    It works, but not good. Is there a better way to do this?

  • Hi @andrewmartin

    I believe the issue occurred because you have an extra double quote for the value there:

    'value' => '"' . get_the_ID() . '""',

    Could you please change it to this one:

    'value' => '"' . get_the_ID() . '"',

    Thanks 🙂

  • @acf-support thanks for the attempt but that wasn’t it. I’ve tried a few different combinations.

    So, here’s more details:

    /register is a page with ID of 2
    /events is a page with an ID of 127.

    The field in the database shows up as:
    a:1:{i:0;s:3:"127";}

    Isn’t it because the LIKE query finds the ID of 2 (as well as the ID of 127) are found in both spots?

  • @acf-support

    I should say, the idea is to have this data / loop _only_ show up on the “LOCATION” page (the page with Id of 127) and as it stands, it’s showing up on both pages.

  • Hi @andrewmartin

    It shouldn’t match both 2 and 127 because it has extra double quotes there:

    '"' . get_the_ID() . '"'

    For example, if get_the_ID() returns 2, then the query would be like this:

    'value' => '"2"',

    Which will only match serialized string that has "2" in it like this:

    a:1:{i:0;s:1:"2";}

    And not this one:

    a:1:{i:0;s:3:"127";}

    Could you please test it again by providing the value manually like this:

    'meta_query' => array(
        array(
            'key' => 'location',
            'value' => '"2"',
            'compare' => 'LIKE'
        )
    ),

    Also, could you please check the database if there’s extra ID there?

    Thanks 🙂

  • @acf-support

    Thanks for your help so far! Tell you what, here’s a quick screencast showing you. I set it to 2; the /register is the page of 2.

    http://cloud.believelabs.com/3V0A1J3l0q2N/Screen%20Recording%202016-08-03%20at%2023.07.gif

    Also, here’s a screenshot from the Database table.

    http://cloud.believelabs.com/2p333R1s3o0R/Image%202016-08-03%20at%2023.08.37.png

  • Hi @andrewmartin

    I’m afraid the screencast doesn’t help much 🙁

    First, I want to make sure that this is not an issue caused by other plugins/theme. Could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Sixteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.

    If the issue persists, could you please open a new ticket and provide temporary admin credentials to your site? You can open a new ticket here: https://support.advancedcustomfields.com/new-ticket. Also, please don’t forget to explain the issue again and provide the link to this thread.

    Thanks 🙂

  • @acf-support

    Looks like it’s an issue with my theme. I’ll create a new ticket. Is there any way I can privately provide admin without giving the credentials on the forum for anyone to see?

  • Hi @andrewmartin

    You can set your reply visibility to “Private” if you want to share sensitive data. But I suggest you open a new ticket and share it there instead.

    Also, please keep in mind that ACF doesn’t provide support for third-party plugins or themes. If the issue related to your theme, please getting in touch with your theme author instead. I believe the issue occurs because there’s a function in your theme that modifies the query. Could you please ask the theme author regarding this issue?

    Thanks 🙂

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

You must be logged in to reply to this topic.