WordPress query to fetch links under a given category


// ‘links’ is the slug name of the link category link

$linkeQry=”SELECT * FROM wp_links as link INNER JOIN wp_term_relationships ON (link.link_id = wp_term_relationships.object_id)

INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) AND wp_term_taxonomy.taxonomy = ‘link_category’

INNER JOIN {$wpdb->prefix}terms as c ON c.term_id=wp_term_taxonomy.term_id

WHERE c.slug=’links'”;

$linksData = $wpdb->get_results($linkeQry);



<?php foreach($linksData as $key=>$link) {  ?>

<li><a href=”<?php echo $link->link_url; ?>” target=”_blank”><?php echo $link->link_name; ?></a></li>

<?php } ?>



WordPress query to list archives


$archiveQry= “SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts

FROM $wpdb->posts  GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC “;

$archivesData = $wpdb->get_results($archiveQry); foreach($archivesData as $key=>$archive) {  ?>

<a href=””><?php echo date(“M”, mktime(0, 0, 0,$archive->month, 1, $archive->year));  ?>

<?php echo $archive->year  ?> </a>

<?php } ?>


Beginners' Guide , PHP Programmer , Zend Certified Engineer , Magento Certified Developer