@Aricus

Как в Wordpress сделать выборку по ACF типа «Записи», возвращающего массив объектов?

Есть произвольное поле ACF типа "Записи", которое организует связь между двумя типами записей. Выборка со стороны, где есть это поле, делается легко. Но как сделать обратную выборку, то есть получить список записей, у которых в этом поле выбрана данная запись?
Если произвольное поле ACF возвращает одно значение, например, строку, то выборку по нему можно сделать с помощью get_posts - meta_query. Но для поля типа "Записи" этот вариант не подойдёт, потому что он возвращает что-то типа этого (секрет за***н):
var_dump(get_field("solution", get_the_ID()));
array (size=2)
0 =>
object(WP_Post)[7912]
public 'ID' => int 29
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2019-04-16 12:10:10' (length=19)
public 'post_date_gmt' => string '2019-04-16 09:10:10' (length=19)
public 'post_content' => string '' (length=0)
public 'post_title' => string 'Водяные тёплые полы' (length=36)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'open' (length=4)
public 'ping_status' => string 'open' (length=4)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'water' (length=5)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2019-06-11 12:29:32' (length=19)
public 'post_modified_gmt' => string '2019-06-11 09:29:32' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://***' (length=32)
public 'menu_order' => int 0
public 'post_type' => string 'post' (length=4)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
1 =>
object(WP_Post)[7913]
public 'ID' => int 31
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2019-04-16 12:11:20' (length=19)
public 'post_date_gmt' => string '2019-04-16 09:11:20' (length=19)
public 'post_content' => string '' (length=0)
public 'post_title' => string 'Электрические тёплые полы' (length=48)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'open' (length=4)
public 'ping_status' => string 'open' (length=4)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'electro' (length=7)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2019-06-11 12:28:16' (length=19)
public 'post_modified_gmt' => string '2019-06-11 09:28:16' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://***' (length=32)
public 'menu_order' => int 0
public 'post_type' => string 'post' (length=4)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)

Нужно искать по массиву среди ->ID или ->post_title. Я понимаю, что можно просто обежать циклом все записи данного типа и выбрать нужные, но тут могут быть проблемы с производительностью. Также можно сделать аналогичное поле с другой стороны, но тогда увеличится труд контент-менеджеров и возрастёт вероятность ошибки. Есть ли лучшее решение?
  • Вопрос задан
  • 252 просмотра
Решения вопроса 1
ya-vitaliy
@ya-vitaliy
Верстаю... + wordpress и пробую Laravel
$posts = get_posts(array(
							'post_type' => 'your_post_type',
							'meta_query' => array(
								array(
									'key' => 'solution', // name of custom field
									'value' => '"' . get_the_ID() . '"',
									'compare' => 'LIKE'
								)
							)
						));

Подробно здеся:
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы