Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to dynamically list all of CPT's in a dropdown, set one in the widget settings and then echo that value in the actual widget output. I can't seem to get it to work though. I was trying to use get_post_types which might be the correct way to achieve this, but I couldn't manage to use the value in the actual widget.

My Base Widget code is:

    <?php
    /**
     * Plugin Name: Custom Search: Resources
     * Plugin URI: http://cbdwichita.com
     * Description: Creates a custom search widget for a specified CPT.
     * Version: 1.0
     * Author: Austin Biggs
     * Author URI: http://cbdwichita.com
     */

    /**
     * Add function to widgets_init that'll load our widget.
     * @since 0.1
     */
    add_action( 'widgets_init', 'cbd_search_resources_widget' );

    /**
     * Register our widget.
     * 'Example_Widget' is the widget class used below.
     *
     * @since 0.1
     */
    function cbd_search_resources_widget() {
        register_widget( 'CBD_Search_Resources' );
    }

    /**
     * Example Widget class.
     * This class handles everything that needs to be handled with the widget:
     * the settings, form, display, and update.  Nice!
     *
     * @since 0.1
     */

    class CBD_Search_Resources extends WP_Widget {

    /**
     * Widget setup.
     */
    function CBD_Search_Resources() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'cbd_search_resources', 'description' => __('Displays a Search for Resources.', 'example') );

        /* Widget control settings. */
        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'cbd-search-resources' );

        /* Create the widget. */
        $this->WP_Widget( 'cbd-search-resources', __('CBD Search Resources', 'cbd_search_resources'), $widget_ops, $control_ops );
    }

    /**
     * How to display the widget on the screen.
     */
    function widget( $args, $instance ) {
        extract( $args );

        /* Our variables from the widget settings. */
        //$number = $instance['number'];
        $title = $instance['title'];
        $tax = $instance['CPT'];




        // start the wordpress loop!
        //if ($query->have_posts()) :  

        /* Before widget (defined by themes). */
        echo $before_widget;

        /* Display the widget title if one was input (before and after defined by themes). */
        //if ( $title )
        echo $before_title . $title . $after_title;
        ?>



        <div class="search-box">
            <form method="get" id="searchform" action="<?php echo home_url() ; ?>/">
                <input type="text" value="<?php echo esc_html($s, 1); ?>" name="s" id="s" maxlength="33" />
                <input id="searchsubmit" type="submit" value="Search">
                <input type="hidden" name="post_type" value="cbd_resources">
            </form>
        </div>



        <?php /* END WHILE AND RESET QUERY */  wp_reset_query(); 


        //else:

        //echo 'There are no upcoming articles.';

        //endif;

        /* After widget (defined by themes). */
        echo $after_widget;
    }

    /**
     * Update the widget settings.
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        //$instance['number'] = strip_tags( $new_instance['number'] );
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['CPT'] = $new_instance['CPT'];
        return $instance;
    }

    /**
     * Displays the widget settings controls on the widget panel.
     * Make use of the get_field_id() and get_field_name() function
     * when creating your form elements. This handles the confusing stuff.
     */
    function form( $instance ) {

        /* Set up some default widget settings. */
        $defaults = array( 'CPT' => '', 'title' => 'Categories');
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>


        <!-- Your Name: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'example'); ?></label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
        </p>



<p>
            <label for="<?php echo $this->get_field_id( 'CPT' ); ?>"><?php _e('Post Type:', 'example'); ?></label>
            <select id="<?php echo $this->get_field_id( 'CPT' ); ?>" name="<?php echo $this->get_field_name( 'CPT' ); ?>" style="width:100%;">
                <?php 
                $args=array(
                  'public'   => true,
                  '_builtin' => false

                ); 
                $output = 'objects'; // or objects
                $operator = 'and'; // 'and' or 'or'
                $postTypes=get_post_types($args,$output,$operator); 
                if  ($postTypes) {
                  foreach ($postTypes  as $CPT ): ?>

                    <option value="<?php echo $CPT->name; ?>" <?php if($instance['CPT'] == $CPT->name) echo "selected='selected'"; ?>><?php echo $CPT->labels->name; ?></option>

                <?php
                  endforeach;
                }
                ?>
            </select>
        </p>


    <?php
    }
    }

    ?>
share|improve this question
    
Which part doesn't work? –  s_ha_dum Jun 7 '13 at 15:01
    
This is currently working, but it's hard coded, I can post what I had, but it just looked like a mess. –  AustinTBiggs Jun 7 '13 at 15:04
    
The broken code that you need to use is probably better than the working code that you aren't going to use, but which method -- function what-- is causing the issue? I'd like to narrow down my debugging. –  s_ha_dum Jun 7 '13 at 15:08
    
I can get the widget settings to save and everything, I just can't seem to use the value in my code. –  AustinTBiggs Jun 7 '13 at 15:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.