WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Post Type Query Issue (8 posts)

  1. heavym
    Member
    Posted 4 years ago #

    Hey All,

    Absolutely loving WordPress 3.0 feature which finally brings it up to par with EE's Channels to make it an out of the box full fledged CMS. Then again, I'm coming from Joomla, so the entire system is so refreshing.

    I've read about 7 articles on custom post types. I've successfully created the function to add it in the functions.php file and shows up properly in the admin. Added an custom post as well.

    Here is the code that pulls in a category to feed the slideshow:

    if (get_option('wdg_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_catId($featured_cat));
        else {
        global $pages_number;
    
        if (get_option('wdg_feat_pages') <> '') $featured_num = $pages_number - count(get_option('wdg_feat_pages'));
        else $featured_num = $pages_number;
    
        query_posts(array
        ('post_type' => 'page',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__not_in' => get_option('wdg_feat_pages'),
        'showposts' => $featured_num
        ));
        };
    
        while (have_posts()) : the_post(); ?>
    
        <div class="slide">
        ">
        <?php
        $post_title = get_the_title();
    
        $thumbnail = get_thumbnail($width,$height,'thumb',$post_title,$post_title);
        $thumb = $thumbnail["thumb"];
    
        print_thumbnail($thumb, $thumbnail["use_timthumb"], $post_title, $width, $height, 'thumb'); ?>
    
        <div class="description">
        <h2>"><?php truncate_title(27); ?></h2>
    
        <?php $tagline = get_post_meta($post->ID, 'Tagline', true);
        if ($tagline != '' ) { ?>
        <p class="tagline">“<?php echo($tagline) ?>”</p>
        <?php } ?>
    
        <p><?php truncate_post(375);?></p>
        " class="readmore"><span><?php _e('Read More', 'WDG');?></span>
        </div> <!-- end .description -->
        </div> <!-- end .slide -->

    This works great. But now I need it to pull in from my new custom post type called featured.

    So I need to incorporate this into the aboves first line:

    global $wp_query;
    $wp_query = new WP_Query("post_type=property&post_status=publish&posts_per_page=5");

    So far no luck. Anyone know the proper syntax to do this? I could provide more information, but giving this for now in case it's a quick solution. Thank you!

    Cheers,
    Christopher

  2. MichaelH
    Member
    Posted 4 years ago #

    Your first said:

    But now I need it to pull in from my new custom post type called featured

    then you used this example:

    $wp_query = new WP_Query("post_type=property&post_status=publish&posts_per_page=5");

    So is it post_type=featured or post_type=property?

  3. heavym
    Member
    Posted 4 years ago #

    Sorry, my bad.

    That code was from the example site. On the site I'm having the issue however I am using post_type=featured.

    wp_query = new WP_Query("post_type=featured&post_status=publish&posts_per_page=5");

    Thank you!

    Cheers,
    Christopher

  4. heavym
    Member
    Posted 4 years ago #

    So ultimately, I need to replace:

    query_posts("showposts=$featured_num&cat=".get_catId($featured_cat));

    with

    WP_Query("post_type=featured&post_status=publish&posts_per_page=5");

    and add global $wp_query; above the if statement.

    So instead of this:

    if (get_option('wdg_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_catId($featured_cat));
    else {

    I would do this:

    global $wp_query;

    if (get_option('wdg_use_pages') == 'false') wp_query = new WP_Query("post_type=featured&post_status=publish&posts_per_page=5");
    else {

    When I do that with debugging on I get, "Parse error: syntax error, unexpected '=' " on the line in question. I tried changing the = to == got a different error and then it started doing the slideshow but with every post in the CMS rather than just the specified post type.

    Also in my functions file I have this:

    /*Add Feature Custom Post Type*/
    register_post_type('featured', array(
    'label' => __('Featured'),
    'singular_label' => __('Feature'),
    'public' => true,
    'show_ui' => true, // UI in admin panel
    '_builtin' => false, // It's a custom post type, not built in!
    '_edit_link' => 'post.php?post=%d',
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => array("slug" => "featured"), // Permalinks format
    'supports' => array('title','page-attributes','custom-fields','editor','thumbnail','excerpt')
    ));

    I'll keep looking and experimenting on my own, but if anyone knows where I should devote my efforts please do!

    Cheers,
    Christopher

  5. heavym
    Member
    Posted 4 years ago #

    I totally figured it out. (Chris goes to buy himself a Starbucks coffee.

    Two things:

    First, thank you Michael again for noting in my post type I had =property. Because even though in my code I have =featured that got me thinking to look in my functions.php and I actually had the post type called "feature" without the d.

    That was certainly an issue, and then I realized at some point I lost the $ in front of the wp_query. So with those two fixes it now works. Woot!

    Cheers,
    Christopher

    PS - I heart WordPress

  6. MichaelH
    Member
    Posted 4 years ago #

    See what this returns:

    <?php
    //get specific post_types, display all published content for each of those types
    $args=array(
      'name' => 'featured'
    );
    $output = 'objects'; // names or objects
    $post_types=get_post_types($args,$output);
    if ($post_types) {
      foreach ($post_types  as $post_type ) {
        $type = $post_type->name;
        $args=array(
          'post_type' => $type,
          '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 'List of ' . $post_type->label;
          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();
      }
    }
    
    ?>
  7. heavym
    Member
    Posted 4 years ago #

    Thank you Michael. I actually figured out the issue. Posted my resolution above. But thank you for taking the time to diagnosis. Now I'm tackling meta boxes for custom post types. Wish me luck, I may be back!

    Cheers,
    Christopher

  8. MichaelH
    Member
    Posted 4 years ago #

    Okay missed you had posted back--that's what I get for not refreshing.

Topic Closed

This topic has been closed to new replies.

About this Topic