Ready to get started?Download WordPress


[resolved] Archive of (only) custom post type, sorted by category (6 posts)

  1. Tom
    Posted 1 year ago #


    What I want:
    I want my frontpage to show all posts (without pagination) in a custom post type, and sorted by category.

    How I thought it would work:

    1. Create custom post type (done, thanks to Bones)
    2. Create an archive page and filter the custom post type (done, thanks to this code I found on wpbeginner)
    3. Somehow use Google to know how to sort this by category.

    What I have for now:
    I have this code (the custom post type is 'machine_type'):

    		global $query_string;
    		query_posts($query_string . "post_type=machine_type&post_status=publish&posts_per_page=999");
    		if ( have_posts() ) : while ( have_posts() ) : the_post();
    		<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
    			<header class="article-header">
    				<h3 class="h2"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    			</header> <!-- end article header -->
    			<section class="entry-content clearfix">
    				<?php the_excerpt(); ?>
    			</section> <!-- end article section -->
    		</article> <!-- end article -->
    		<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    		<?php the_excerpt(); ?>
    	<?php endwhile; endif; ?>
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Next entries') ?></div>
    	<?php wp_reset_query(); ?>

    Questions I have:

    1. Is this the way to go? (Is it possible to achieve what I want in this way?)
    2. Is this code efficient?
    3. How can I sort the result by category?

    I'd prefer not to use a plugin as (I think) this isn't that much code, and it would give me a better insight in wp.

    Can you please help me?

  2. Mayeenul Islam
    Posted 1 year ago #

    You din't mention, whether you tried it or not. If tried, then what's the result? But I think you are on the right track.

    If it won't work, try a category ID instead of the name of the category:
    query_posts( 'cat=3&showposts=99' );

    Just go to Posts > Categories, then put your mouse over the category you want to sort with, look at the bottom of the browser, you will see the ID of the category. A category ID is much better and safer than a category name.

    By default the posts are sorted with posting times (LIFO method). I don't any other way of sorting posts till now other than the date&time.

  3. deepbevel
    Posted 1 year ago #

  4. Tom
    Posted 1 year ago #

    @Mayeenul Islam Thank you for your response. I didn't mention it but I did tried it. It works, but doesn't sort by category (how could it, that isn't in the code if I'm correct). The problem is that I don't know how the number of categories there will be. So it's not possible hard coding it.

    @Deepbevel Thank you for your response. I've already seen that answer, but the problem still stands (my apologies for not explaining it well in the first place): I don't know the number of categories.

    I've uploaded a screen shot of (a part of) my mock up on dropbox, you can find it here: http://dl.dropbox.com/u/66836392/Schermafbeelding%20(7).png.
    Here you see two categories (schocktestkasten and stresskasten) with posts (every image is another post). But I don't know how many categories there will be.

  5. deepbevel
    Posted 1 year ago #

  6. Tom
    Posted 1 year ago #

    @Deepbevel thank you for the link, that solutions works (after I've changed "'post_type' => 'post'," to "'post_type' => 'machine_type'," of course).

Topic Closed

This topic has been closed to new replies.

About this Topic