Skip to main content
  • custom post type:

    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' );
    
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:

    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 );
      }
    
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 );
}
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 = '
'.print_r($home_slides_args, true).'
';
//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;

}

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; }
  • 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 );
}
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 = '
'.print_r($home_slides_args, true).'
';
//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;

}

  • 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 );
      }
    
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;
}
Source Link
Sebastian
  • 105
  • 1
  • 1
  • 9

Query custom posts from custom taxonomy

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 = '
'.print_r($home_slides_args, true).'
';
//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?