Now I’m however getting: Fatal error: Uncaught Error: Undefined
constant “cat”
That’s because the cat
and post_tag
were not wrapped in quotes (e.g. 'cat'
), and they also did not start with the dollar sign ($
) as in $cat
, which indicates a variable, hence PHP tried to find the constants named cat
or post_tag
, and when the constants do not exist or are not defined, then PHP throws the above error. More details here.
So I believe the code should instead be written like so: $term = get_term_by('slug', 'cat', 'post_tag');
.
Also, an additional note: You can actually simply use the found_posts
property of a WP_Query
class instance, to get the total number of posts in your query. Just use 'posts_per_page' => 1
and not 'posts_per_page' => -1
, then use new WP_Query
instead of get_posts()
to get the count. I.e.
/* Replace this:
$posts = get_posts($args);
$countallowed = count($posts);
*/
// with this one:
$query = new WP_Query($args);
$countallowed = $query->found_posts;