Reputation: 26598
I am writing a simple script php. This script has to know if a specified category passed via $_get exist in wordpress cms.
exist.php?cat=xxx
I have read this but I am not sure if it can help me. http://codex.wordpress.org/Function_Reference/is_category
Can anyone help me? How can I do? Thanks.
Upvotes: 0
Views: 2542
Reputation: 593
Use get_categories to get all categories in an array, and then use in_array to find it category exists. You can then use the same category id to get more information about the category too. Reference: http://codex.wordpress.org/Function_Reference/get_categories#Source_File Reference: http://php.net/manual/en/function.in-array.php
Example:
<?php
$cat_list = get_categories();
if (in_array($_GET['cat'], $cat_list)) {
// exists
} else {
// does not exists
}
?>
Upvotes: 2
Reputation: 16656
Since it looks like you are trying to access it from a separate PHP file, connect to the WordPress database in your script and run this query:
<?php
include 'wp-config.php';
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
$stmt = $dbh->prepare( "SELECT * FROM `wp_term_taxonomy` TT RIGHT JOIN `wp_terms` TE ON TT.`term_id` = TE.`term_id` WHERE TT.`taxonomy` = 'category' AND TE.`name` = :category" );
$stmt->bindParam( ':category', $_GET['cat'] );
$stmt->execute();
if ( $row = $stmt->fetch() ) {
// It exists
} else {
// It does not exist
}
Upvotes: 2