0

I have a meta_query where I am getting children of a page, I am trying to only get children where the meta key is not the same as the posts title

$args = array(
  'posts_per_page' => -1,
  'parent'         => $post->ID,
  'post_type'      => 'post_type',
  'post_status'    => 'publish',
  'sort_column'    => 'menu_order',
  'meta_query'     => array(
    array(
      'key'     => 'my_acf_key',
      'value'   => [how do I add the post title here]
      'compare' => '!=',
    )
  )
  $result = new WP_Query($args);

I am willing to do a custom query, but I was hoping there would be a way to do this with the WP_Query function (that and I have no idea how to even think about building the custom query with the joins necessary to get both tables searched)

I would just get them all and filter after the result is fetched, but I am querying a few thousand fields so the time delay is noticeable

2
  • 3
    Unfortunately this simply isn't possible via WP_Query. It's just not smart enough under the hood.
    – vancoder
    Aug 5, 2022 at 20:33
  • Something like get_the_title(wp_get_post_parent_id($post->ID))? Aug 6, 2022 at 1:01

1 Answer 1

0

I found a solution that works (thanks Steve), hopefully somebody will find this useful in the future

$sql = "SELECT p.ID FROM wp_posts AS p
  WHERE post_parent = $post->ID
  AND post_type = 'post_type'
  AND ID IN (
   SELECT post_id FROM wp_postmeta AS pm
     WHERE pm.meta_key = 'my_acf_key'
     AND pm.meta_value = p.post_title
);";

$idList = $wpdb->get_col( $sql );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.