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 do a custo slideshow, but I have problems with query the posts. I have defined a custom post type "slider" and a custom taxonomy "slideshow":

  • custom post type:

    function wms_slider_init() {
    $labels = array(
    'name' => 'Slider',
    'singular_name' => 'Slider',
    'add_new' => 'Add Slider',
    'add_new_item' => 'Add New Slider',
    'edit_item' => 'Edit Slider',
    'new_item' => 'New Slider',
    'all_items' => 'All Sliders',
    'view_item' => 'View Slider',
    'search_items' => 'Search Sliders',
    'not_found' =>  'No Slider found',
    'not_found_in_trash' => 'No Slider found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Sliders'
      );
    
      $args = array(
    'labels' => $labels,
    'description'   => 'Holds our Slider poste specific data',
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array( 'slug' => 'slider' ),
    'capability_type' => 'post',
    'has_archive' => false, 
    'hierarchical' => true,
    'menu_position' => 5,
    'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes')
      ); 
    
      register_post_type( 'slider', $args );
    }
    add_action( 'init', 'wms_slider_init' );
    
  • custom taxonomy:

    add_action( 'init', 'wms_create_slider_taxonomies', 0 );
    
    //create SlideShow Category for the post type "slider"
    function wms_create_slider_taxonomies() 
    {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
    'name'                => _x( 'SlideShows', 'taxonomy general name' ),
    'singular_name'       => _x( 'SlideShow', 'taxonomy singular name' ),
    'search_items'        => __( 'Search Genres' ),
    'all_items'           => __( 'All SlideShows' ),
    'parent_item'         => __( 'Parent SlideShow' ),
    'parent_item_colon'   => __( 'Parent SlideShow:' ),
    'edit_item'           => __( 'Edit SlideShow' ), 
    'update_item'         => __( 'Update SlideShow' ),
    'add_new_item'        => __( 'Add New SlideShow' ),
    'new_item_name'       => __( 'New SlideShow Name' ),
    'menu_name'           => __( 'SlideShow' )
      );    
    
      $args = array(
    'hierarchical'        => true,
    'labels'              => $labels,
    'show_ui'             => true,
    'show_admin_column'   => true,
    'query_var'           => true,
    'rewrite'             => array( 'slug' => 'slideshow' )
     );
    
      register_taxonomy( 'slideshow', array( 'slider' ), $args );
    }
    

I made under taxonomy "slideshow" a "category" named - "homeslide" and I put in it a "slider" named "test". I try to set up a query to retrieve all posts from "homeslide", but the query is not working.

here is my query and response

function wms_output_home_slides($cat) { // $cat = 'homeslide'
  $home_slides_args = array(
            'post_type' => 'slider',
            'posts_per_page'  => -1,
            'tax_query' => array(
            array(
                'taxonomy' => 'slideshow',
                'field' => 'slug',
                'terms' => $cat
                   )
                 )
);
$sliders_query = new WP_Query( $home_slides_args );
//$print_out = '<pre>'.print_r($home_slides_args, true).'</pre>';
//loop
if ($sliders_query->have_posts()) : 
    $print_out .= '<div id="pageBackground">';
    while  ($sliders_query->have_posts()) : $sliders_query->the_post();
        $slider_id          = $post->ID;

        $print_out .= '<div class="one-slide">';

            $print_out .= $slider_id;

        $print_out .= '</div><!-- one-slide -->';
    endwhile;
    $print_out .= '</div><!-- #pageBackground -->';
endif;


return $print_out;
}

Any Ideea why didn't get the post ID?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You should access global variables in php using global keyword.It makes variable( or object, array) visible inside current function we are dealing with. In function wms_output_home_slides you are using $post global variable directly instead access it using global keyword as following.

function wms_output_home_slides($cat) { // $cat = 'homeslide'
global $post;
// You Code Goes here
}

Find more information on this page.

share|improve this answer
    
One magic line :) –  Sebastian Mar 7 '13 at 9:54
add comment

Your Answer

 
discard

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

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