0

I'm using a WP_Query inside one of my functions.

function get_id_list ($postType) {
    $wpb_all_query = new WP_Query(
        array(
            'post_type' => $postType,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                    //'key' => 'title',
                    //'value' => 'Wordpress Development BSP Project', // this works and the function only returns a list with the single ID of the post where this matches
                    'key' => 'bicslab_axis',
                    'value' => '22', // this does not work
                    'value' => 'greenware', // this does not work

                )
            )
        )
    );
    $postIDList = [];
    if($wpb_all_query->have_posts()){
        while ( $wpb_all_query->have_posts() ) {
            $wpb_all_query->the_post();
            $postIDList[] = get_the_ID();
        }
        wp_reset_postdata();
    }
    print_r($postIDList);
    return $postIDList;
}

The query works correctly when in the 'meta_query' I choose a key which is a field of type "text", but it does not if I choose a field of type "relationship".

Here is a screenshot of my advanced custom fields: enter image description here

For the relationship fields I tried putting the ID and the name, none of them would work.

How would I do it to make the query retrieve only posts where custom field of type relation is related to only some objects?

Edit: I should clarify, I used "22"respectively "greenware" as values in my mea query because 22 is the ID and greenware is the "name" of the specific blog post/object with that id

charelf
  • 3,103
  • 4
  • 29
  • 51

2 Answers2

1

It's because you're searching for a string in an object.

Using WP_Query arguments

It is possible to load only the selected post ID’s, instead of the post objects. This way, you can use the ID’s within a WP_Query and specify arguments such as posts_per_page, order and orderby. To learn more about the WP_Query arguments, please read http://codex.wordpress.org/Class_Reference/WP_Query#Parameters.

Note that the get_field function has 2 false parameters. The first param is for the $post_id and is not relevant, but the second one is to tell ACF not to format the value, and return only what is in the DB (array of IDs)

<?php 

// get only first 3 results
$ids = get_field('conference_talks', false, false);

$query = new WP_Query(array(
    'post_type'         => 'conferences',
    'posts_per_page'    => 3,
    'post__in'          => $ids,
    'post_status'       => 'any',
    'orderby'           => 'post__in',
));

?>

Source: https://www.advancedcustomfields.com/resources/relationship/

Gavin Thomas
  • 1,196
  • 6
  • 10
  • I've been working with your code and experimenting with it - without succes.. Just so we're talking about the same thing, what would you add to my query that so that it works? I have set the relationship to return post ID's not post objects. (https://imgur.com/d2BzC6F) thus, if your $id works similar, it would return a string containing the id. In that case, why would my hardcoded string "22" not work? – charelf Aug 14 '18 at 12:48
  • Yeh I had a look and seen you managed to figure it out, I played with `'IN'`, because relationship fields would return arrays but still no luck. – Gavin Thomas Aug 14 '18 at 13:32
  • BTW, now that you saw my possible solution, do you perhaps know why it works? https://wordpress.stackexchange.com/questions/70864/meta-query-compare-operator-explanation this is an explanation of the `LIKE` operator, but I do not understand what the `value` is. Based on my understanding I can just deduce that the `value` starts with 22, but I have no idea how to find out more information about it. – charelf Aug 14 '18 at 13:39
  • Not without doing more digging. It may be worth querying all posts and `var_dumping` the field to see what it's returning - based on that you should be able to find a strong answer (would require a lot of setting up to do on my side ^^). – Gavin Thomas Aug 14 '18 at 13:41
  • I did so now and at first sight the output does not heko me much. Here is the `var_dump` of `$wpb_all_query`: https://justpaste.it/4v1dc **edit:** this was with 22 as the value, i.e. the code being exactly like in my answer below **edit 2:** no sorry the value was 23, which is the ID of another object – charelf Aug 14 '18 at 13:46
  • 1
    Exclude meta information from the query, then `var_dump(get_field('bicslab_axis', $ID))` on one of the fields. – Gavin Thomas Aug 14 '18 at 13:55
  • I commented out the meta query and executed this: `var_dump(get_field('bicslab_axis', "62"));` (I used 62 instead of 22, as 62 is a post object that has the bicslab_axis field where as the post object 22 does not have one. More precisely, the custom post type of ID 62 has the field bicslab_axis of type relationship, and the object it is related to is a custom post type with ID 22.) anyway, here is the output: `array(1) { [0]=> int(22) }` – charelf Aug 14 '18 at 14:03
  • 1
    Alright I've just dont some research - it looks like your solution is the correct way to query. (see bottom: https://www.advancedcustomfields.com/resources/querying-relationship-fields/) – Gavin Thomas Aug 14 '18 at 14:22
  • Oh I see, thank you very much for all the work you put into this. I guess I'll have to work with the `LIKE` operator then. – charelf Aug 14 '18 at 14:26
0

I found a solution: WordPress query posts by ACF

The only things I needed to change was the 'value' and 'compare' parameters, from this:

'value' =>'22',
'compare' => '=',

to this:

'value' =>'"'.'22'.'"',
'compare' => 'LIKE',

edit: it even works without the forced quotation marks:

'value' => '22',
'compare' => 'LIKE',

Here is the complete code.:

function get_id_list ($postType) {
    $wpb_all_query = new WP_Query(
        array(
            'post_type' => $postType,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                    'key' => 'bicslab_axis',
                    'value' => '22',
                    'compare' => 'LIKE',

                )
            )
        )
    );
    $postIDList = [];
    if($wpb_all_query->have_posts()){
        while ( $wpb_all_query->have_posts() ) {
            $wpb_all_query->the_post();
            $postIDList[] = get_the_ID();
        }
        wp_reset_postdata();
    }
    print_r($postIDList);
    return $postIDList;
}
charelf
  • 3,103
  • 4
  • 29
  • 51