WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Post Types and Categories (15 posts)

  1. hatefulcrawdad
    Member
    Posted 4 years ago #

    Hello, I'm wondering if there is a way to filter the custom post types by category. I currently have a post-type called "portfolio" and a custom taxonomy on it called "Project Type" with 'print', 'web', and 'identity' as options to select.

    I want to have a page that only shows the most recent "portfolio" posts with the Project Type of Web, etc. I can't seem to figure this one out. Is this an option at this point? Any help would be great because Google failed me so far.

  2. Agus Suhanto
    Member
    Posted 4 years ago #

    If you want a 'page' that shows only certain post type from from certain category, one thing to achieve it is using custom page template (not the default one).

    With custom page template you should do anything you want. The downside is it requires programming skill.

    Other way is using a plugin. But the downside is still the same.

  3. hatefulcrawdad
    Member
    Posted 4 years ago #

    I realize it going to take some programming skills. I just want en explanation of terms or tags that will filter by category of a custom post type.

    I'm currently using this code to pull out all posts in the "portfolio" post type. But I want to limit what it shows to the Project Type of 'print' or 'web', etc.

    <?php $port = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => 1 ) );
    while ( $port->have_posts() ) : $port->the_post(); ?>
    
      <h2><?php the_title(); ?></h2>
    
      <?php the_content(); ?>
    
    <?php endwhile; ?>
  4. MichaelH
    Member
    Posted 4 years ago #

    You can't do that yet with 'standard' WordPress. Look for the plugin http://wordpress.org/extend/plugins/query-multiple-taxonomies/

  5. hatefulcrawdad
    Member
    Posted 4 years ago #

    That plugin doesn't really do what I need. I just want to show posts that include the taxonomy "web" or something like that. But these posts are from a custom post type. It must be possible, why would WP leave that out?

  6. hatefulcrawdad
    Member
    Posted 4 years ago #

    should 'post_type' => 'portfolio', 'Project Type' => 'Print' work?

  7. MichaelH
    Member
    Posted 4 years ago #

    On my test 'post_type' => 'book', 'genre' => 'mystery' works.

  8. hatefulcrawdad
    Member
    Posted 4 years ago #

    I cannot seem to get that to work. I wonder if my custom post code is wrong somewhere. Can you tell me how you went about creating your custom post? Did you use a plugin or go into function.php?

  9. hatefulcrawdad
    Member
    Posted 4 years ago #

    Here is the code I'm using to get my custom post type and taxonomy. Does it look right?

    add_action('init', 'portfolio');
    function portfolio()
    {
      $labels = array(
        'name' => _x('Portfolio', 'post type general name'),
        'singular_name' => _x('Portfolio', 'post type singular name'),
        'add_new' => _x('Add New', 'project'),
        'add_new_item' => __('Add New Project'),
        'edit_item' => __('Edit Project'),
        'new_item' => __('New Project'),
        'view_item' => __('View Project'),
        'search_items' => __('Search Projects'),
        'not_found' =>  __('No projects found'),
        'not_found_in_trash' => __('No projects found in Trash'),
        'parent_item_colon' => ''
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 4,
    	'_builtin' => false,
        'supports' => array('title','editor','thumbnail','custom-fields','cats')
      );
      register_post_type('portfolio',$args);
    }
    
    register_taxonomy( 'media', 'portfolio', array( 'hierarchical' => false, 'label' => __('Media Type'), 'query_var' => 'media' ) );

    Here is the code I'm using in my page file to try and get the 'media' => 'print' to pass through.

    <?php query_posts( array( 'post_type' => 'portfolio', 'media' => 'print' ) );
    while (have_posts() ) : the_post(); ?>
    
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <?php endwhile; ?>
  10. hatefulcrawdad
    Member
    Posted 4 years ago #

    *bump* sorry, but I'm stressing over this and just want it to work already. Anyone?

  11. MichaelH
    Member
    Posted 4 years ago #

    See if a new query gets it:

    <?php
    $args=array(
      'media' => 'print',
      'post_type' => 'portfolio',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'Portfolio/media print';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Note--that 'cats' in your supports argument in not valid.

  12. hatefulcrawdad
    Member
    Posted 4 years ago #

    I tried the new query and it just sends all the posts in "portfolio" through. Hmmm. Does anything else look wrong in my code?

  13. hatefulcrawdad
    Member
    Posted 4 years ago #

    Should I try creating my custom post type with a plugin or something? Can you send all the code you used on the test page you did? So I can compare it to mine, or try it on my installation?

  14. MichaelH
    Member
    Posted 4 years ago #

    Used the code from the examples in Function_Reference/register_post_type and Function_Reference/register_taxonomy in the theme's functions.php to register the post_type and taxonomy.

    Then with this code in the index.php just after this line:

    <div id="content" role="main">

    to display books assigned the mystery genre.

    <?php
    $args=array(
      'genre' => 'mystery',
      'post_type' => 'book',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'Books in the genre=mystery';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  15. hatefulcrawdad
    Member
    Posted 4 years ago #

    OK, i got it fixed. I just redid the code again and it worked, strange. Something was off somewhere. Now I'm having problem with my links, when i click on the post name I get a 404 error. I guess I should post a new thread for that.

Topic Closed

This topic has been closed to new replies.

About this Topic