A
A
Anton2020-07-07 13:03:04
WordPress
Anton, 2020-07-07 13:03:04

Why doesn't WordPress WP_Query() output links from the ACF field?

In WordPress ACF there is a field for pages of the type "Link to page" (page_link) I try to filter all pages where a certain link is specified and nothing happens. I write code like this:

$args = array(
  'post_type'  => 'page',
  'meta_query' => array(
    array(
      'key' => 'razmer',
      'value' => 'https://домен/400mmx40mm/',
    )
  )
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
  while ( $query->have_posts() ) {
    $query->the_post();
    echo '<li>' . get_the_title() . '</li>';
  }
} else{
  echo 'Ничего';
}


It does not find anything, although there is a page with such a field and address.
Maybe in value it is necessary to pass not a reference?
What am I doing wrong, can anyone tell me?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Chesnokov, 2020-07-07
@cesnokov

The "page_link" type field stores the page ID and not its URL (page exception in the Archive).
UPDATE:

$post_id = url_to_postid('https://домен/400mmx40mm/');
$args = array(
  'post_type' => 'page',
  'meta_query' => array(
    array(
      'key' => 'razmer',
      'value' => $post_id,
    )
  )
);

A
Anton, 2020-07-07
@a22432

Thanks yarovikov and Pavel Chesnokov
Should have written:

$args = array(
  'post_type'  => 'page',
  'meta_query' => array(
    array(
      'key' => 'razmer',
      'value' => 289,
      'compare' => 'LIKE'
    )
  )
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
  while ( $query->have_posts() ) {
    $query->the_post();
    echo '<li>' . get_the_title() . '</li>';
  }
} else{
  echo 'Ничего';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question