Let ChatGPT evaluate your WordPress code

When coding, a nice use case for ChatGPT is to ask it to evaluate my code.

I normally prompt it to be an “experienced WordPress backend developer” and then tell it what to do.

In the following example, I knew that my query was missing something to make it use OR, but I didn’t remember the exact parameter. I used this chance to test ChatGPT and only asked it to evaluate my code in general.

ChatGPT not only saw in the comments what my intention was, but also had other ideas to improve the code. Not all of them were relevant, but something an experienced WordPress developer might stumble upon as well.

The only thing it didn’t catch was that WordPress serializes empty arrays, which then look like this in the meta value: a:0:{}.

Btw., Google’s Bard did not catch anything and just told me how “well written” my function was.

See my prompt and ChatGPT’s reply below.

Prompt

You are an experienced WordPress developer. Evaluate the following function:

/**
 * Get all attachments that are not used
 * read: they don’t have the ìsc_image_posts` meta value set up or it is an empty array.
 *
 * @return WP_Post[] Array of post objects or post IDs.
 */
public function get_unused_images() {
	$args = [
		'post_type'   => 'attachment',
		'numberposts' => ISC_Model::MAX_POSTS,
		'post_status' => null,
		'post_parent' => null,
		'meta_query'  => [
			// the image does not have the meta information or it is an empty array
			[
				'key'     => 'isc_image_posts',
				'compare' => 'NOT EXISTS',
			],
			[
				'key'     => 'isc_image_posts',
				'value'   => '',
				'compare' => '=',
			],
		],
	];

	return get_posts( $args );
}Code language: PHP (php)

Reply

The function get_unused_images() is designed to retrieve all attachments that do not have the isc_image_posts meta key set or, if it’s set, it has an empty value. Let’s evaluate its components:

  1. Function Comment (DocBlock):
    • Clear and explains well the purpose of the function.
    • Clarifies that the result can be either an array of post objects or post IDs.
  2. $args Array:
    • The post_type is set to ‘attachment’, which means the function is specifically querying for media attachments.
    • The numberposts parameter limits the number of returned posts to the constant value ISC_Model::MAX_POSTS.
    • post_status and post_parent with a null value might be redundant as they don’t specifically impact the query for attachments.
    • The meta_query is well-structured to ensure the attachments either don’t have the meta key or have it with an empty value. However, there’s a slight issue here; the current setup will only return posts that match both conditions (logical AND operation), whereas you likely want posts that match either condition (logical OR operation).
  3. Function Return:
    • The function always returns an array, which is consistent with the function comment. However, the comment also mentions it could return post IDs, but the function, in its current state, will always return post objects.

Recommendations:

  1. In the meta_query, you should add a 'relation' => 'OR' to get the correct posts:
    'meta_query' => [ 'relation' => 'OR', [ 'key' => 'isc_image_posts', 'compare' => 'NOT EXISTS', ], [ 'key' => 'isc_image_posts', 'value' => '', 'compare' => '=', ], ],
  2. Consider removing the post_status and post_parent with null values unless you have a specific purpose for including them.
  3. If you wish to have the function also possibly return post IDs, you would need to add an argument such as fields => 'ids' to the $args array. However, you’d need to handle this conditionally based on the requirements of where the function is used.
  4. Always consider caching heavy queries. If this query is frequently used and the ISC_Model::MAX_POSTS has a large value, this might impact performance. Implementing a transient caching system would be beneficial.

Overall, the function is clear and concise, but with a few minor modifications, it can be made more efficient and consistent with its documentation.