WordPress: How to get the post count for a term and post type

The WordPress function get_term returns the count for the number of posts in the term, but this is for all post types.  You need a custom function to count the number of posts in a term for a specific post_type.

/**
* Returns the number of posts for a term in a taxonomy, for a post type
* @param string $post_type
* @param string $taxonomy
* @return int count
*/
function get_post_count_for_term_and_post_type( $term_id, $taxonomy, $post_type ) {

  // Build the args
  $args = array(
    'post_type' => $post_type,
    'posts_per_page' => -1,
    'tax_query' => array(
      array(
        'taxonomy' => $taxonomy,
        'field' => 'id',
        'terms' => $term_id,
      )
    )
  );

  // Get the posts
  $posts = get_posts( $args );

  // Return the count
  return count($posts);

}

 

 

 

Moving from a 2017 iMac 5K (and MBP) and a 2021 MacBook Pro M1 Pro

I’m excited by the new 2021 MacBook Pro computers.  They are a great evolution from the 2020 models, and the performance and battery gains from the Intel machines are impossible to ignore.

I’m planning to replace an iMac and MacBook Pro with a new M1 MacBook Pro.

My iMac

I have a 2017 5K iMac which has been the best computer I have ever owned.  I bought it expecting it to last me 5 to 10 years.  Well, it’s been nearly 5 years and until the M1 chips came along I had no reason to think about changing it.

The performance is incredible, and the screen is still the best you can buy.

But this computer will now devalue quickly (because of the new M1 chips), and I want to get what I can for it.

My 2015 MacBook Pro

I also have a 2015 MacBook 15″ Pro.  I used to have the 13″ version which was the best laptop I had ever owned, but upgraded to the 15″ as I mainly used it when on site with clients, and the screen space was valuable.  I also used it occasionally as my work from home computer when I had an office in Brighton, where the iMac lived.

This laptop is quite big and the fans run loud even with the slightest load on the CPU. I even used a fan control app to reduce the spinning.

The new 2021 MacBook Pro M1

This new laptop is amazing.  17 hours of battery life, the very fast M1 Pro processor, MagSafe, HMDI, 3 x Thunderbolt/USB ports.  It’s a return to the great Apple laptops of the past.

Replacing the iMac

The idea was to buy the M1 laptop and replace the iMac and old MBP with the single computer.  I don’t work at multiple locations anymore, I rarely work on site with clients, and I need a more powerful laptop for Serato DJ software.

The iMac is my main monitor and I have an old Thunderbolt display as my second monitor.

So I just buy a new monitor to replace the iMac and I’m set, right? Well, no.

I’ve now discovered just how much the iMac does, and how much needs to be done to replace it.  It’s not just a computer with a screen.  Here’s what it really is:

  1. Desktop computer
  2. 5K monitor
  3. Thunderbolt, USB, SD card and Ethernet hub
  4. Excellent speakers
  5. Webcam
  6. Keyboard
  7. Mouse

To replace the iMac I’ve bought the following

  1. LG 27″ 4K display

I already have

  1. Keyboard
  2. Mouse
  3. Thunderbolt Display will act as USB and Ethernet hub (and possibly webcam)
  4. The laptop has a webcam (better than the display) and SD card reader.

Who knows, one day I might get another iMac for my home office as well as the M1 laptop, but I’m glad to removing the need to sync files between computers.  I used DropBox for this, and I’m keen to not use DropBox anymore.

How to Fix Zero Bounce Rate of all Sessions on Google Analytics when using Events

If you are experiencing a zero bounce rate across all of your sessions in Google Analytics and you are using Events then check the following.

The problem

Google Analytics considers a session to be a bounce when only a single page is viewed and there is no user interaction.

If you are sending events to Google Analytics after the page has loaded this is considered an interaction, and therefore the session will never be a bounce.  If you never have bounced sessions the bounce rate will always be zero.

The solution

Here is an example command sending an event to Google Analytics.  This is considered interaction with the session, and therefore will never be a bounce,

ga('send', 'event', 'Category', 'Action', 'Label');

Below is an example with the nonInteraction property set to true.  Google Analytics will record the event but will not consider this user interaction.  If the user only views a single page in the session then this will count as a bounce.

ga('send', 'event', 'Category', 'Action', 'Label', {

  nonInteraction: true

});
Here’s a great resource which helped me find this solution.

Weeknotes 5

  • I’m creating a new WordPress site based on a classic ASP site.  The old site still works and I’m importing the data from the .mdb database.  The old site is showing its age as the layout and images are all tiny.  It’s not responsive either.  Although the design is so narrow it probably looks fine in a modern mobile.
  • I love starting a new website (see above).  Especially one with lots of content types and templates.
  • I do not love the final stages of a website.  Feedback from the client, and time consuming niggles are my least favourite thing to manage.
  • I guess, for me, the measure of a successful website creation is the lack of feedback/changes from a client, as this shows clear requirements understanding and execution.
  • I’ve returned to using a Pomodoro technique to try and increase my productivity and reduce my distractedness.  I’m using the Be Focused Mac app, mainly because it was free, but might change to something else.  Im finding the break time especially good for clearing off small non-work admin tasks,
  • With just a few weeks to go until the Brighton Half Marathon I did a 10 mile run this week.  I don’t have my form of a few years ago, but I’m happy to be outside running in the fresh air.  I’ve switched to daytime runs this year, rather than nighttime, and it’s allowed me to enjoy it a lot more.  A great perk from home working.

Weeknotes 4

  • I started using Bitbucket Pipelines to deploy projects.  I’m a great fan of Gitlab due to their awesome CI system, but have only just started to use Bitbucket Pipelines to auto deploy my sites.  I’ve setup several sites this week on Bitbucket to deploy.  Check out my Gists for some example CI scripts.
  • I have only worked with one new client in perhaps the last year, with all of my work coming from existing clients.  This means I can add more value to this projects I’ve worked on in the past.
  • A had an enquiry from a company who are currently having a new website built and enquired if I would support it and providing training after it was delivered.  I find this an odd situation as I provide all of the support and maintenance for every website I have ever made.  I thinks it’s odd that a developer wouldn’t off this, and I find it stranger than a client would accept this from a developer.
  • The Gift Project that I worked on in 2018 and 2019 is presenting at more Museums.  This is a fantastic project and it makes me very happy to see it adopted by more Museums.

Weeknotes 3

I’ve been watched the BBC’s Severn Worlds, One Planet. The production quality is astonishing, as is the human impact on the planet.

I’ve enjoyed working on several different projects this week. I do this every week, sometimes working on several in a day.

I’m very keen to finish a long winded web project I’ve been working on.

I’ve started my half marathon training.

I’ve started eating mince pies and chocolate.

Weeknotes 2

This week I did a bunch of updates on the optilabs.com and listenandlearnresearch.com websites.  These are both sites I have worked on for years.  I really value continued relationships with clients, as it’s good regular work that takes little time and investment to win.

I was contacted by a travel agency owner who needed to update an ageing Drupal 6 site as it has come to its end of life, and also their only member of staff who knew the site was leaving.  I had a long call with him, and expressed my opinion that Drupal is hard to use and develop for.  I put together a fixed price to incorporate all the required work to migrate to WordPress.  This was brave for me as the brief was minimal.  He decided to stick with Drupal, going with a different freelancer/agency. He seemed to realise this was perhaps the wrong move, as Drupal had not served them well.  Price so often drives peoples short term decisions, even when they know they are facing the same problems long term.

I created a new freelance WordPress developers guide website.  This is a site I had toyed with several years ago, but then it got archived in a server move.  I’ve found myself with lots of tips to journal and got the site back online.

I’m finding more time to focus on my long overdue side projects, which feels very satisfying.

Weeknotes 1

Inspired by Mark Boulton and others I’ve decided to try writing weekly notes, as a record of things I’m working on and other little snippets.

I worked on DKIM signing outgoing emails for the itvworkplaceservices.com website.  A little outside my normal wheelhouse, but aways nice to learn new things.

Last week was the Spit Spreads Death parade in Philadelphia.  This wonderful parade was a memorial to the 20,000 people that lost their live in 1918 to a flu pandemic.  The parade was created by Blast Theory, who I regularly collaborate with.  I worked on a React and Node.js app to allow people to choose who they will represent in the parade. I also contributed to another React app used during the parade.

I discovered a client was using a website that they never paid me for. Long story, but basically I was to develop sister websites for sister companies. One client pulled out at the end, but eventually just took a copy of the other website. I’ve emailed the client but haven’t heard back yet.

 

How to change WooCommerce product gallery zoom image size

By default the WooCommerce product gallery zoom image size is very big.  This can cause the zoomed image to look distorted and too far from the original.

The cause is that WooCommerce selects the ‘full’ image size to use as the zoomed image.

Use the snippet below to the use the ‘medium’ image size as the zoom image.  ‘large’ will give a bigger zoomed image.

// Change the image size used for the WooCommerce product gallery image zoom
// 'medium' or 'large' are normally the best options
add_filter( 'woocommerce_gallery_full_size', function( $size ) {
  return 'medium';
} );

How to enable Hot Reloading in React Native

By default Hot Reloading in React Native only works if you extend React.Component.
If you are using your own base classes to inherit from you need to change the .babelrc file.

Below is my working example. In the superClasses section you will see BasePanel and InfoPanel which are my base classes.

{
 "presets": ["babel-preset-expo"],
 "env": {
  "development": {
    "plugins": [
      ["react-transform",
        {
          "transforms": [{
             "transform": "react-transform-hmr",
             "imports": ["react"],
             "locals": ["module"]
           }],
          "superClasses": ["BasePanel", "InfoPanel", "React.Component", "Component"]
        }
      ]
     ]  
    }
  }
}

Latest from the blog

View All
Social media & sharing icons powered by UltimatelySocial