Using AI to improve work

Mar 9, 2023 | Development, Learning, WordPress

Artificial intelligence (AI) is all the rage now, and we decided to give it a go with something practical.

We do a lot of custom development using Custom Post Types and Custom Taxonomies. This would entail the writing of custom queries to display the correct information onto various pages.

So, for this we asked ChatGPT the following:

“WordPress query for custom posts of current custom terms”

The response was near perfect:


 

ChatGPT

Assuming you have a custom taxonomy named custom_taxonomy and a custom post type named custom_post_type, you can use the following code to get all the custom posts associated with the current custom term:

<?php
$current_term = get_queried_object(); // Get the current custom term
$args = array(
   'post_type' => 'custom_post_type',
   'tax_query' => array(
       array(
          'taxonomy' => 'custom_taxonomy',
          'field' => 'slug',
          'terms' => $current_term->slug
      )
   )
);
$custom_posts = new WP_Query( $args ); // Query the custom posts
if ( $custom_posts->have_posts() ) {
   while ( $custom_posts->have_posts() ) {
      $custom_posts->the_post();
      // Output your custom post content here
   }
}
wp_reset_postdata(); // Reset post data to default
?>

In this code, we first use get_queried_object() to get the current custom term. Then we create an array of arguments for WP_Query that specifies the post_type and tax_query parameters. The tax_query parameter is an array of arrays that specifies the taxonomy and term we want to filter by. Finally, we use WP_Query to query the custom posts and loop through them using have_posts() and the_post().

Note that we also use wp_reset_postdata() to reset the post data to the default after the loop, which is good practice to avoid conflicts with other queries on the page.


 

What we found…..

The user would still have to update the code to replace the ‘custom_post_type’ and ‘custom_taxonomy’ with the correct values as defined on the website, else it would just not work; or break the site.

As a start, the user would also need to know what to ask or how to compose the question to get to the right answer.  The user would then need to know what to do with this information or where to put it – either in theme templates or create a shortcode to display it on a page.  Lastly front-end styling of the returned information would have to be done.

From our experiment it is clear that OpenAI will make life / work easier for those embracing it, but there will always be some human interaction / tweaking required, no matter how advanced it gets.

Please share your comments below.

Related posts

Percentage discounts in WooCommerce

Recently a client needed a 25% discount on their WooCommerce website.  Pretty simple to do; either by manually updating each product or using a bulk editor. A month later the same client wanted the discount to be removed and the regular price set to reflect the same...

read more

Pin It on Pinterest

Share This