4

Suppose we have few categories, tags and custom post type and taxonomies. How can we list all categories, tags in a page? Something like;

Page

Category Thumbnail and Category Name 1(link to category page) Description

Category Thumbnail and Category Name 2(link to category page) Description

Category Thumbnail and Category Name 3(link to category page) Description

Divider

Tag Thumbnail and Tag Name 1(link to Tag page) Description

Tag Thumbnail and Tag Name 2(link to Tag page) Description

Tag Thumbnail and Tag Name 3(link to Tag page) Description

Same with custom post type and taxonomies.

And if the description and thumbnail are not possible then only a name with link will be adequate.

Most of this is achievable by these plugins.

Right now I'm looking in plugin code to achieve what I want.

3

TERM METADATA

It's possible to get thumbnails for terms using the new Term Metadata in 4.4. You just also need to define those ahead of time yourself.


CATEGORIES

get_categories ( ) Returns an array of category objects matching the query parameters.

echo "Categories:<br/>";

$args = array( 
    'orderby'                  => 'name',
    'order'                    => 'ASC', 
    'public'                   => true,
); 

$categories = get_categories( $args );

foreach ( $categories as $category ) {
     echo '<a href="' . get_category_link( $category ) . '">' . $category->name . '</a><br/>';
}

echo "<hr>"; // divider

TAGS

get_tags ( ) Retrieves an array of objects for each term in post_tag taxonomy.

echo "Tags:<br/>";

$args = array( 
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'public'                   => true,
); 

$tags = get_tags( $args );

foreach ( $tags as $tag ) {
     echo '<a href="' . get_tag_link( $tag ) . '">' . $tag->name . '</a><br/>';
}

TAXONOMIES

get_taxonomies ( ) Get a list of registered taxonomy objects.

echo "Taxonomies:<br/>";

$args = array(  
    '_builtin'                 => false, 
    'public'                   => true,
); 

$taxonomies = get_taxonomies( $args, 'objects' ); 

foreach ( $taxonomies as $taxonomy ) {
    echo '<p>' . $taxonomy->labels->name . '</p>';
}

POST TYPES

get_post_types ( ) Returns the registered post types as found in $wp_post_types. get_posts ( ) creates an array of posts based on a set of parameters so use it with your post_type_list.

echo "Post Types:<br/>";

$args = array(
    'public'   => true,
    '_builtin' => false,
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types($args, $output, $operator);

// get all the posts for all post_types

$posts = get_posts(array(
                           'post_type'   => $post_types,
                           'numberposts' => - 1,
                           'orderby'     => 'post_type',
                           'order'       => 'ASC',
                           'post_status' => array( 'publish' ),
                           'public'      => true,
                       ));

foreach($posts as $post) {

    $link = get_permalink($post);
    $title = $post->post_title;

    if($post_type !== $post->post_type) {
        $post_type = $post->post_type;
        echo '</br/><p>' . $post_type . '</p></br/>';
    }

    // show link to post

    echo "<p><a href=\"$link\">$title</a></p>";
}

TRANSIENT CACHING

If you want to cache the results of all these queries then consider the Transients API. This will store the result into the database for a limited time (which you ~request~). It's not a certain timeframe because plugins like WP Optimize can clear the cache anytime.

// check if the value exists using the key -- if it fails then we'll generate the content

if(false === ($trans_value = get_transient($trans_key = 'my_transient_key'))) {

    print_r('NO CACHE FOR ' . $trans_key); // (debug output)

    // start the output buffer to capture our generated content

    ob_start();

    // run your expensive operations -- data sent to the screen will be captured

    print_r('This is an expensive operation!');

    // using ob_get_clean we'll store the results and set the value variable.

    // double check the amount of time you want to keep the data cached
    // set to 0 for auto loading on every page hit.

    set_transient($trans_key, $trans_value = ob_get_clean(), 1 * MINUTE_IN_SECONDS);
}
else {
    print_r('THIS IS A CACHED RESULT FOR ' . $trans_key); // (debug output)
}

// value is ready and cached
print_r("<pre>$trans_value</pre>");

To clear the value just delete using the key.

delete_transient ( $trans_key );
  • Note: Passing term id's to get_*_link() functions are expensive. get_term_link() (which is used by all get_*_link() functions) queries the db to get the term object via get_term_by(). You should always, where possible (like in your examples), pass term objects to get_*_link() functions. This way, these functions do not need to make a db call to get the term object – Pieter Goosen Dec 28 '15 at 5:54
  • Also, your function to get posts according to post type is quite expensive, you can do all of that in one query – Pieter Goosen Dec 28 '15 at 6:15
  • The type parameter in get_categories does not relate to the post type, but to the type of taxonomy, either category or link_category. This parameter is depreciated since the introduction of custom taxonomies in version 3.0, and IIRC, it used to accept only link as value which meant it should get terms of the taxonomy link_category – Pieter Goosen Dec 28 '15 at 6:32
  • I didn't see your notes on codex.wordpress.org/WordPress_Optimization/…. – jgraup Dec 28 '15 at 15:06
  • @PieterGoosen I think there is no problem in finding better solution, if anyone have better solution they can edit answer if possible, give new answer or in comment. Community should get benefit, right/wrong doesn't matter. – Anshuman Chandel Feb 13 '16 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.