0
votes

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

2

2 Answers

1
votes

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/

0
votes

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;
}