WordPress.org

Ready to get started?Download WordPress

Forums

How to Separate Custom Post Type Search Results into Tabs? (8 posts)

  1. redwall
    Member
    Posted 2 years ago #

    Hello,

    I have a WordPress Installation. I have about 3 x custom post types on my site. When I do a search, all post type results get displayed on the front end on 1 page. Some post types are totally different from others content wise so the results look a bit funny when they are all displayed together.

    Is there a way to split the results into tabs according to post type? I want to leave the results as default the way they are now (All Results) but then have tabs to the right filtering out the different results according to post type they belong to.

    I created a jpeg here to help explain exactly what I am trying to do.

    Thanks

    http://f.cl.ly/items/110S2K1A0T3m290Y0C14/Filter_post_Types.jpg

  2. Shail
    Member
    Posted 2 years ago #

    May this function help you

    http://codex.wordpress.org/Template_Tags/get_posts

    In this function pass your post type slug in argument.
    'post_type' => 'type your custom post type slug here'

    Using this function you will get posts from specific post types.

  3. redwall
    Member
    Posted 2 years ago #

    Hi Shall, thanks for the input. It might very well help but I am a bit of a noob with PHP so could do with a little more specific guidance if you can?

    My idea was to use tabs, so are u saying that clicking each tab would render out a new search (kind fo do a refresh) ? That could work of course. Ultimate might be a JS method so no page refresh if that is possible..

  4. Shail
    Member
    Posted 1 year ago #

    You can call all the custom post type data and after than use display none to other tabs using css.

    Now when you click on another tab you can display this tab block and other tabs none using jquery its very simple you can google it lots of example available for tab..

  5. redwall
    Member
    Posted 1 year ago #

    Hi Again,

    I was planning on using shortcodes as tabs

    [tabs]
    [tab]All Search Results[/tab]
    [tab]Post Type 1 Results[/tab]
    [tab]Post Type 1 Results[/tab]
    [tab]Post Type 1 Results[/tab]
    [tab]

    or in PHP Template I can also do it by

    <?php 
    
    echo do_shortcode('[tabs style="boxed" id="searchTabs"]
       [tab title="Post Type 1"]Post Type 1 Results[/tab]
       [tab title="Post Type 2"]Post Type 2 Results[/tab]
       [tab title="Post Type 3"]Post Type 3 Results[/tab]
       [/tabs]');
    
    ?>

    I took a look at the code above you suggested , pasted it below. But I am not sure how to combine it all together with my tabs and the code below.

    <?php
    $the_slug = 'my_slug';
    $args=array(
      'name' => $the_slug,
      'post_type' => 'custom_post_Type_1',
      'post_status' => 'publish',
      'numberposts' => 1
    );
    $my_posts = get_posts($args);
    if( $my_posts ) {
    echo 'ID on the first post found '.$my_posts[0]->ID;
    }
    ?>

    If you can guide me a little further that would be awesome.

    Cheers for the help

  6. Shail
    Member
    Posted 1 year ago #

    This is the code for display two different custom post type in tabs.

    <?php
    $the_slug = 'my_slug';
    $args=array(
      'name' => $the_slug,
      'post_type' => 'custom_post_Type_1',
      'post_status' => 'publish',
      'numberposts' => 1
    );
    $my_posts = get_posts($args);
    
    $the_slug2 = 'my_slug2';
    $args2=array(
      'name' => $the_slug2,
      'post_type' => 'custom_post_Type_2',
      'post_status' => 'publish',
      'numberposts' => 1
    );
    $my_posts2 = get_posts($args2);
    
    echo do_shortcode('[tabs style="boxed" id="searchTabs"]
    
       [tab title="Post Type 1"]
            <?php //This code is for first post type
            foreach( $myposts as $post ) :	setup_postdata($post); ?>
    
                <li><a>"><?php the_title(); ?></a></li>
    
            <?php endforeach; ?>
       [/tab]
    
       [tab title="Post Type 2"]
           <?php //This code is for second post type
           foreach( $myposts2 as $post ) :	setup_postdata($post); ?>
    
                <li><a>"><?php the_title(); ?></a></li>
    
            <?php endforeach; ?>
       [/tab]
    
       [/tabs]');
    ?>
  7. redwall
    Member
    Posted 1 year ago #

    Hi Shail,

    Thanks for sticking in there with me to solve this, really appreciate it.

    I tried myself inserting that code into my PHP template for search results but I get a mess on my end, I must be doing something wrong.

    My search.php template code is exactly this http://pastie.org/4248751

    The search page I am trying to modify looks exactly like this
    http://demo.woothemes.com/listings/?field_1=0&s=car&submit=Search+Site

    THis is the demo site of the theme I am using so is pretty much same as what I have on my localhost.

    From those 2 pieces of information is there a way you could help me edit that pastie to achieve the right code implementation?

    Thanks a lot

  8. redwall
    Member
    Posted 1 year ago #

    Hi Shail,

    Was wondering if u had a chance to check out the links I posted above?

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic