Anton Nesterenko2020-01-11 02:08:18
Anton Nesterenko, 2020-01-11 02:08:18

advanced custom fields. What value to choose for post_object?

I have a project where I have product categories and products themselves. I need to display products in a certain category through a custom field.
I used acf and created a post_object on the product page. but with this that I came up with, I managed to display all the products where no category options were selected at all

$args = array(
            'numberposts'	=> -1,
            'post_type'		=> 'watch-page',
            'order' => 'ASC',
            'meta_query'	=> array(
                'key'		=> 'category_to_select',
                'compare' => '!='

          // query
          $the_query = new WP_Query( $args );

What value do I need to specify in order to display products in the category you choose?
Tried 'value' => $cat or 'value' => $cat->ID - didn't work
Thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Maxim Morev, 2020-01-11

The task is solved in a strange way - ACF is generally useless here.
You have a custom post type (watch-page), create and link a custom taxonomy to it (for example, watch-page__tax), create the necessary categories.
If everything is done correctly, then in the admin panel when creating a post on the right, you can specify the desired category (or several).
Well, then

$query = new WP_Query([
  'post_type' => 'watch-page',
  'tax_query' => [
      'taxonomy' => 'watch-page__tax',
      'field'    => 'name',
      'terms'    => 'Название категории'

Anton Nesterenko, 2020-01-11

Maxim Morev thanks for the help, opened my eyes for me
It was just necessary to create a taxonomy with the_loop standard loop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question