Search code examples
phpsqlwordpressadvanced-custom-fields

filter posts query by post object value


I have a custom post type with a post object field that enables the selection of related pages for the custom post type, the post object field is enabled for multiple posts, and is set to return post id.

I want to build a query in a specific page that checks all the custom post types that are related to this page with post object.

I am using this query:

$val = get_the_ID(); // get current page ID

$args = array(
'post_type'     => 'articles_content', //custom post type
'posts_per_page'    => -1,
'meta_query'        => array(
        'key' => 'pages_link',  //the name of the post object field
        'value' => $val,  //the value of the current ID
        'compare' => 'LIKE' 
        ),
);

$the_query = new WP_Query( $args );

if( $the_query->have_posts() ):

    while( $the_query->have_posts() ) : 

        $the_query->the_post();
        // Do something...

    endwhile;   

endif;

wp_reset_query();

The problem is this - for examlpe if my page ID is "80", then the query also brings custom posts that are have pages with ID of "8002" or something similar. I tried to use the "=" sign in the compare but got no results at all.

How can I query only by the custom posts that have the exact page ID as the current page in their post object array values?


Solution

  • This is the solution if anyone encounters the same issue:

    $args = array(
    'posts_per_page'    => -1,
    'post_type'         => 'articles_content',
    'meta_query' => array(
                                array(
                                    'key' => 'pages_link', 
                                    'value' => '"' . get_the_ID() . '"', 
                                    'compare' => 'LIKE'
                                )
                            )
    );