creating wordpress post’s selectbox / dropdown sorted by category

Home » Blog » creating wordpress post’s selectbox / dropdown sorted by category

Following function will create wordpress post’s selectbox / dropdown sorted by category.

Add the following code in your theme’s function.php file

function display_post_selectbox($class_name='') {

	global $wpdb, $post;
	$table_prefix = $wpdb->prefix;

	$the_output = NULL;
	$last_posts = (array)$wpdb->get_results("
		SELECT {$table_prefix}terms.name, {$table_prefix}terms.term_id
		FROM {$table_prefix}terms, {$table_prefix}term_taxonomy
		WHERE {$table_prefix}terms.term_id = {$table_prefix}term_taxonomy.term_id
                AND {$table_prefix}term_taxonomy.taxonomy = 'category'
		{$hide_check}
	");
	if (empty($last_posts)) {
		return NULL;
	}
	$used_cats = array();;
	$i = 0;
	foreach ($last_posts as $posts) {
		if (in_array($posts->name, $used_cats)) {
			unset($last_posts[$i]);
		} else {
			$used_cats[] = $posts->name;
		}
		$i++;
	}
	$last_posts = array_values($last_posts);

	$the_output .= '<select name="wordpress_posts" class="'.$class_name.'">';
	foreach ($last_posts as $posts) {

	  $the_output .= '<optgroup label="'.apply_filters('list_cats', $posts->name, $posts).'">';

          $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'" , $r );

          $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select object_id FROM {$table_prefix}term_relationships, {$table_prefix}terms WHERE {$table_prefix}term_relationships.term_taxonomy_id =" . $posts->term_id . ") ORDER BY post_date DESC");

	  foreach ( $arcresults as $arcresult ) {
	       $the_output .= '<option label="'.apply_filters('the_title', $arcresult->post_title).'" value="'.apply_filters('the_title', $arcresult->post_title).'">'.apply_filters('the_title', $arcresult->post_title) . '</option>';
	   }

          $the_output .= '</optgroup>';
       }
       $the_output .= '</select>';
       return $the_output;
}

To display the selectbox add following code in your theme’s php file where you want to display it.

<?php
if(function_exists('display_post_selectbox'))
{
  echo display_post_selectbox();
}
?>

Tags:

8 Comments on "creating wordpress post’s selectbox / dropdown sorted by category"

  1. Rachael Parker says:

    I’ve tried this but the drop down doesn’t appear

    Reply →
  2. TheGeek says:

    Is it possible to make something like this that can work together with .

    I need the possibility to split a post (hardware review) in several pages, and next page does that fine.

    But now I’m looking for a function that can make an dropdown with the pages, with an title like

    Introduction
    Bundle
    Hardware
    Etc

    Is that possible to make at all… ?

    Reply →
  3. TheGeek says:

    I need a solution I can add to the post’s so they have more pages with nice title

    Right now i have the next page with numbers and a drop down with the numbers to.

    The wish is drop down with page titles/names so the reader can chose/see what the next page is.

    http://www.anandtech.com/show/4133/amd-gseries-brings-apus-to-the-x86-embedded-market

    In the bottom of the posts they have a nice link, and a nice drop down, that is the one i need

    Reply →
  4. shankan says:

    Thank you,
    that is work well.

    Reply →

Got something to say? Go for it!

How to remove the http://t.co/XhUQ8T6p link from Login / Register Page: On self hosted wordpress blogs wordpress ... http://t.co/LWDOuEfq - 1 week ago