Ahmad Awais

NAVIGATE


SHARE


EDD Taxonomy Terms: Include/Exclude and Redux Select Field

Ahmad AwaisAhmad Awais

Today while working over a premium WordPress theme I had to solve two problems. First of all I needed to build a custom WordPress query through which I could display posts from a particular term while excluding other particular terms. Secondly, I wanted to provide end-user with a select-2 based drop down menu to select from the present terms of a custom taxonomy.

Context#

Think of a theme which displays three different blocks of posts on the homepage. Basically, I am building a theme for Easy Digital Downloads plugin. Now think again, at the homepage you need to add three different blocks of posts from three different terms of EDD’s Categories. Where EDD’s categories are based upon a custom taxonomy calledΒ download_category.

Data Set#

Building a custom WPQuery#

First of all I needed to build a custom query. While building custom WPQuery for taxonomy you need to realize that it is pretty different from simple queries of custom post types. Here one needs to build an array for tax_query argument. Inside this array we define the relation of two more arrays of posts where former is responsible for selecting the posts from a term and later array is responsible for excluding posts from defined terms. Here is how I did it.
Where term ID 7 is the Featured term ID and 5,6 are for Recent and Deals terms respectively.

Adding aΒ terms to Redux field type: select#

I use Redux Framework to provide my theme users with a set of options in form of a Theme Settings page. Luckily Redux offers a select-2 based field type called select. I wanted to provide theme users with a drop down panel from where they could select a particular category Β for a particular block i.e. term from a custom taxonomy.

All I had to do was build a custom arguments array toΒ define which particular custom taxonomy should be used to populate the select-2 based drop-down menu. So, I went ahead and started reading the docs for select field. I must say Dovy Paukstys (@SimpleRain) has done a pretty good job with Redux framework and its documentation. Though at first, I was not able to make much sense out of it. Then after several iterations I finally found a solution which I intend to document today. Β  Β 
Look at line #14, I have added an array where I defined the custom taxonomy which should be used to populate terms in the select drop down. This is how it looks inside theme options.

Terms-in-redux-select

Final Form of WPQuery#

Finally here is whatΒ WPQuery for Block #1 looks like

That’s about it. If you have any questions, let me know.

Founder & CEO at Langbase.com Β· Ex VP DevRel Rapid Β· Google Developers Advisory Board (gDAB) founding member. πŸ§‘β€πŸ’» AI/ML/DevTools Angel Investor ❯ AI/ML Advisory Board member San Francisco, DevNetwork

🎩 Award-winning Open Source Engineer & Dev Advocate 🦊 Google Developers Expert Web DevRel πŸš€ NASA Mars Ingenuity Helicopter mission code contributor πŸ† 8th GitHub Stars Award recipient with 3x GitHub Stars Award (Listed as GitHub's #1 JavaScript trending developer).

🌳 Node.js foundation Community Committee Outreach Lead, Member Linux Foundation, OpenAPI Business Governing Board, and DigitalOcean Navigator. πŸ“Ÿ Teaching thousands of developers Node.js CLI Automation (100 videos Β· 22 Projects) & VSCode.pro course. Over 142 Million views, 22 yrs Blogging, 56K developers learning, 200+ FOSS.

✌️ Author of various open-source dev-tools and software libraries utilized by millions of developers worldwide ⓦ WordPress Core Developer πŸ“£ TEDx Speaker with 100+ international talks.

✨ As quoted by: Satya Nadella Β· CEO of Microsoft β€” Awais is an awesome example for developers.
πŸ™Œ Leading developers and publishing technical content for over a decade πŸ’œ Loves his wife (Maedah) ❯ Read more about Ahmad Awais.

πŸ‘‹β€¦ Awais is mostly active on Twitter @MrAhmadAwais

πŸ“¨

Developers Takeaway

Stay ahead in the web dev community with Ahmad's expert insights on open-source, developer relations, dev-tools, and side-hustles. Insider-email-only-content. Don't miss out - subscirbe for a dose of professional advice and a dash of humor. No spam, pinky-promise!

✨ 172,438 Developers Already Subscribed
Comments 2
  • Isu
    Posted on

    Isu Isu

    Reply Author

    What if I want get posts from Taxonomy and there are must be from different post type?
    Eg.
    ‘type’ => ‘select’,
    ‘data’ => ‘posts’,
    ‘args’ => array(‘taxonomy’ => array(‘download_category’)),
    But this not work, it show posts from default posts.


    • Ahmad Awais
      Posted on

      Ahmad Awais Ahmad Awais

      Reply Author

      I didn’t really get your question. Posts from taxonomies are already available at their archive pages.