When working on third-party plugins or a purchased theme, we may encounter a situation where we need to attach an existing taxonomy (created by someone else) to a custom post type we’ve created (in our case, “fruits” — see this article).

Order website or plugin development for WordPress, website development on the Laravel, Symfony, or Yii2 framework…

When working on third-party plugins or a purchased theme, we may encounter a situation where we need to attach an existing taxonomy (created by someone else) to a custom post type we’ve created (in our case, “fruits” — see this article).

As an example, let’s review a small piece of code that implements a tree with fruits attached to it. In this case, the 'tree' is our custom taxonomy, and 'fruit' is a custom post type used to define fruits with all their parameters (color, taste, etc. — these are not discussed in this article).

To retrieve a specific post type in WordPress filtered by a custom (or built-in) taxonomy, use the following snippet:
$Posts = get_posts(array( 'post_type' => 'my-post-type', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'my-taxonomy', 'field' => 'slug', 'terms' => 'event' ) ), 'meta_query' => array( 'AND', array( 'type' => 'NUMERIC', 'key' => 'event_date', 'compare' => '<', 'value' => time() ), array( 'type' => 'NUMERIC', 'key' => 'is_archive', 'compare' => '==', 'value' => 0 ) ) ));