简体   繁体   中英

How to create a query that return custom post type by Advanced Custom Field(ACF) Taxonony?

you all!

I have categories of Author

  • 1 - Freelancer
  • 2 - Contract
  • 3 - Half-time

I have categories of Books

  • 1 - Novel
  • 2 - Fiction

I have a custom post type Author. Author has custom fields:

  • Name: Text
  • Type: Category

I have a custom post type Book. Book has custom fields:

  • Title: Text
  • Author: Taxonomy
  • Type: Category
  • Pages: Number

I need a query that return ALL BOOKs with Authors that have Contract ID = 1

And

I need a query that return ALL BOOKs with Type = Fiction

Thanks!!!

You should use meta_query to get the desired results:

I need a query that return ALL BOOKs with Authors that have Contract ID = 1

$freelance_books = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'books',
    'meta_key'      => 'book_contract', // Adjust here with your field name
    'meta_value'    => 1
));

I need a query that return ALL BOOKs with Type = Fiction

$fiction_books = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'books',
    'meta_key'      => 'book_type', // Adjust here with your field name
    'meta_value'    => 2
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM