Get Terms By Custom Post Types And Taxonomy In WordPress

The following snippet saved me a bunch time when dealing with a recent project. The scenario was I created 2 custom post types and they share the default “category” taxonomy with Posts. On the post type archive page, I have to display the category links so users can filter the posts.

The difficult part was I wanted to only show categories that have posts in the current post type. The following snippet came from StackExchange, with the function hook to the terms_clauses filter, now we can use a custom argument called post_types in the function get_terms.

The post_types can be an array so you can get terms like this:

get_terms( array( 'post_types' => array('post', 'cpt1', 'cpt2'), 'taxonomy' => 'category' ) );

or just:

get_terms( array( 'post_types' => 'cpt1', 'taxonomy' => 'category' ) );

Please go to StackExchange to upvote the answer if you find it helpful to you too.

I'm a senior web developer helping clients build their websites to grow businesses. Currently I'm based in Taipei, Taiwan. I write things about WordPress, AngularJS and life. Whenever you'd like to find someone to talk about these topics, just get in touch!

Changing Arguments For The Built-In Taxonomies In WordPress

With the following snippet you can change arguments for the built-in taxonomies in WordPress:

To modify arguments for the built-in taxonomies might not be a good idea unless you really know what you’re doing. I’m doing this because (not sure since when) now WordPress won’t let me display links from the link_category taxonomy (remember Links Manager?) on its own template page (taxonomy-link_category.php).

One symptom I’ve noticed is that when I visited the link_category term page (like visiting http://SITE.COM/?link_category=links), it showed the index page template but not 404 page, which meant such route did exist just the data couldn’t be shown public.

After some time-wasting trial and error I realized it’s the default arguments keep the template from working. And the snippet above did the trick perfectly.

Hope it could save someone thirty or so minutes someday in the future. 😉

I'm a senior web developer helping clients build their websites to grow businesses. Currently I'm based in Taipei, Taiwan. I write things about WordPress, AngularJS and life. Whenever you'd like to find someone to talk about these topics, just get in touch!

Switch To Another Theme On Certain Pages In WordPress

I wrote a tiny WordPress plugin to switch themes between certain pages and the rest ones of a site. Check the gist out:

Here’s my brief explanation:

  1. Line 15-19: We need to hook to these two filters (stylesheet and template) to let WordPress know we’d like to use another theme other than the one has been set. I’d recommend we do such calls in setup_theme action. Maybe you could use other actions instead, if you find one, just let me know.
  2. Line 21-28: This is our filter function to change stylesheet WordPress would use. If you’re going to use a child theme like me, set your child theme here, or just use your theme.
    And the most important part is how I tell WordPress to switch themes on “certain pages”. I specify them by checking the $_SERVER['REQUEST_URI'] param.
  3. Line 30-37: Basically works like the previous one, just it changes the template (parent theme).

The use case of this plugin is that for a client site I built and maintain, I need to enable a survey form which will be built by Gravity Forms. Though it won’t be too difficult to build such form, I felt lazy to write some more CSS to style it. I was just wondering, it would be so much less work for me to do if I can use the default WordPress theme to display the form.

And after some googling, it’s done! I hope it could help you with your needs too.

 

I'm a senior web developer helping clients build their websites to grow businesses. Currently I'm based in Taipei, Taiwan. I write things about WordPress, AngularJS and life. Whenever you'd like to find someone to talk about these topics, just get in touch!

Save Dev Machine Space By Displaying Images From Production Server

Be sure to check out the original post. If you’re on Nginx, just use the following gist for the images URL rewriting.

I'm a senior web developer helping clients build their websites to grow businesses. Currently I'm based in Taipei, Taiwan. I write things about WordPress, AngularJS and life. Whenever you'd like to find someone to talk about these topics, just get in touch!

Getting Categories With Posts In A Single WP API Request

UPDATED March 03, 2016:

Post has been updated. Now we use "rest_prepare_category" filter for this job.

UPDATED March 02, 2016: 

With the latest WP API V2 Beta12, the filter "rest_prepare_term" I used in this post no longer work and it should be changed to "rest_prepare_category". I'll update the post content ASAP.

A while back ago I wrote a post to show you how to “Adding Fields To The JSON Response Of The WP REST API“. In the first scenario why we’ll ever need to add fields to WP API, is for people who want to build an accordion menu with categories as the first level links, which when being clicked, the posts would be collapsed as the second level links.

We know we could use WP API to get a single category info, and we can also get the posts for a certain category, but unfortunately we can’t do both in a single API request. Read More…

I'm a senior web developer helping clients build their websites to grow businesses. Currently I'm based in Taipei, Taiwan. I write things about WordPress, AngularJS and life. Whenever you'd like to find someone to talk about these topics, just get in touch!

Setting Page Title Dynamically In AngularJS WordPress Theme

Changing the page title dynamically is an important feature in our AngularJS WordPress Theme. If you have checked some articles related to this topic, you might find it’s a bit hard to integrate those methods to our theme. There are two reasons:

  1. We just moved the ng-app="app" from html to #page div, that means the title element is beyond the scope of our app. Most of the tutorials use the $scope.title approach to change page title, unfortunately it won’t work with our theme.
  2. Most tutorials will predefine a title for each route, for example there are 3 files a.html, b.html and c.html, and their titles are like Page A, Page B and Page C. In such scenario it’s much easier to get and set the page title on $routeChangeSuccess event. But our case is a bit different that we’ll need to get a post from WP API first to know what the title is.

This post from stackoverflow gave me an idea to change the title with the global variable document. Here’s the snippet I use to change the page title dynamically (line 7, line 14).

Note that I use document.querySelector('title').innerHTML rather than document.title, so special characters like HTML entities won’t be decoded. You can get full project files from the GitHub repo.

If you have any question related to my AngularJS WordPress theme series, just leave a comment here, I’ll get back to you as soon as I can.

I'm a senior web developer helping clients build their websites to grow businesses. Currently I'm based in Taipei, Taiwan. I write things about WordPress, AngularJS and life. Whenever you'd like to find someone to talk about these topics, just get in touch!