Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active September 11, 2018 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/c8eb7e636f9cd42f80908a94af7946a6 to your computer and use it in GitHub Desktop.
Save tommcfarlin/c8eb7e636f9cd42f80908a94af7946a6 to your computer and use it in GitHub Desktop.
[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