Okay so I need to get all posts to which the current post is related to. This is set through a relationship field on the queried posts, not on my current one. But the quirk is that the relationship field is nested within a repeater field for repeating blocks on a page. So within my "block_repeater" there is a relationship field "block_relations" and my Query looks like this:
$args = array(
'post_type' => array('post', 'page'),
'meta_key' => 'block_relations',
'value' => '"' . $post->ID . '"',
'compare' => 'LIKE',
);
$q = new WP_Query($args);
This query returns nothing and I think it's because of the nesting. In my database "postmeta" table the entry for "block_relations" looks like "blocks_0_block_relations", "blocks_1_block_relations", and so on. Is there a way to do this in one query as opposed to querying all posts that have the "blocks" repeater and then foreach getting my field in question and testing whether it has the appropriate value??
I've found the solution and it's not pretty. The problem is you have to go by the keys "blocks_0_block_relations" but the metaquery searches for meta_key with equals(=) operator so you have to make a custom filter to filter out your specific query and alter it to use LIKE comparison. solution:
$args = array(
'post_type' => array('post', 'page'),
'meta_query' => array(
array(
'key' => 'blocks_$_block_relations',
'value' => $post->ID,
'compare' => 'LIKE',
)
),
);
$q = new WP_Query($args);
And the custom filter to alter the where clause:
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'blocks_$", "meta_key LIKE 'blocks_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');