Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Adding Custom Information to a WordPress Category Edit Page
<?php
add_action('edit_category_form', 'acme_display_object_ids');
/**
* Displays an ordered list of all of the object IDs for a given
* taxonomy.
*/
function acme_display_object_ids()
{
// Code for displaying the Object IDs goes here.
}
<?php
class ObjectIdSubscriber
{
/**
* @var string the hook to which this class subscribes
*/
private $hook;
/**
* @param string $hook the hook to which this class is registered with WordPress
*/
public function __construct(string $hook)
{
$this->hook = $hook;
}
/**
* Renders all of the information for the object IDs.
*/
public function load()
{
// Code for displaying the Object IDs goes here.
}
}
<?php
// This will retrieve all of the term relationships from where we can get post IDs.
global $wpdb;
$termTaxonomyId = $wpdb->get_results(
$wpdb->prepare(
"
SELECT DISTINCT term_taxonomy_id
FROM $wpdb->term_taxonomy
WHERE term_id = %s
",
$tagId
),
ARRAY_A
);
$termTaxonomyId = isset($termTaxonomyId[0]) ? $termTaxonomyId[0] : null;
if (null === $termTaxonomyId) {
return null;
}
<?php
// This will retrieve all of the post IDs.
$postIds = $wpdb->get_results(
$wpdb->prepare(
"
SELECT object_id
FROM $wpdb->term_relationships
WHERE term_taxonomy_id = %s
",
$termTaxonomyId
),
ARRAY_A
);
if (empty($postIds)) {
return null;
}
<ul>
<?php foreach ($postIds as $postId) : ?>
<li><?php echo $postId; ?></li>
<?php endforeach; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.