V
V
vika_tulacity2021-10-14 17:43:39
WordPress
vika_tulacity, 2021-10-14 17:43:39

How to display custom posts (Custom Post Type UI) via WP_Query by custom field (ACF)?

Help, please, deal with WP_Query.

Now the output of posts (the personal slug) looks like this:

<?php $loop = new WP_Query( array( 'post_type' => 'personal', 'posts_per_page' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
  <?php echo the_post_thumbnail_url(); ?>
  <?php echo the_title(); ?>
  <?php echo the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_query();?>


This is how all posts of type personal are displayed. I also need to configure the output by department.

Each post has custom fields created through the ACF plugin, of type "checkbox" - otdel with values:

buhuchet: Accounting
kadry: Human Resources
juristy: jurisprudence
nalogoobl: Taxation
experts: Expert support

I need to display, for example, posts of type personal, where the checkbox "buhuchet: Accounting" is checked

How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-10-15
@artzolin

Try like this

$args = [
  'post_type' => 'personal',
  'posts_per_page' => 20,
  'meta_query' => [
    [
      'key' => 'otdel',
      'value' => '"buhuchet"',
      'compare' => 'LIKE'
    ]
  ]
];

$loop = new WP_Query( $args );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question