Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Post Type Query by category and group WordPress

  • SOLVED

HELP! I'm not even sure how to wrap my head around this and still learning wordpress. I need to be able to query against a custom post type (resources) and sort posts by categories with the corresponding heading. The list is sorted first by category, then in alphabetical order. I also want the category title to show up above the first post of that category. Similar to:

CATEGORY (EX: Hand Man)
Post 1 - Name (title) / Phone (phone) / Email (email)
Post 2 - Name (title) / Phone (phone) / Email (email)
Post 3 - Name (title) / Phone (phone) / Email (email)

CATEGORY (EX: Mortgage Broker)
Post 1 - Name (title) / Phone (phone) / Email (email)
Post 2 - Name (title) / Phone (phone) / Email (email)
Post 3 - Name (title) / Phone (phone) / Email (email)

CATEGORY (EX: Another Category)
Post 1 - Name (title) / Phone (phone) / Email (email)
Post 2 - Name (title) / Phone (phone) / Email (email)
Post 3 - Name (title) / Phone (phone) / Email (email)

Any help is much appreciated!!!! Thank you in advance.

Answers (5)

2014-03-02

Hariprasad Vijayan answers:

Hi,

You can try this method

<?php $catargs = array(
'orderby' => 'name',
'order' => 'ASC',
);
$categories = get_categories( $catargs );
foreach ($categories as $category) {?>
<h3><?php echo $category->name;// Category title ?></h3>
<?php
// WP_Query arguments
$args = array (
'post_type' => 'resources',
'cat' => $category->cat_ID,
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'title',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<h5><?php the_title(); // Post title ?></h5>
<?php
// You can all phone/ email here
}
} else {
// no posts found
}

// Restore original Post Data
wp_reset_postdata();
}

If you just need to display 3 posts, you can change 'posts_per_page' to 3.

If phone/ email are custom fields, you can call it using get_post_meta().

Let me know if you need any help from me.


phxbulldog comments:

Forgive me, but I'm still learning wordpress. I can't get your chunk of code to work properly.


<?php $catargs = array(
'orderby' => 'name',
'order' => 'ASC',
);

$categories = get_categories( $catargs );
foreach ($categories as $category) {?>

<h3><?php echo $category->resource_types; // Category title ?></h3>

<?php
// WP_Query arguments
$args = array (

'post_type' => 'resources',
'cat' => $category->cat_ID,
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'title',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>

<h5><?php the_title(); // Post title ?></h5>

<?php get_post_meta( 'email' ); ?>
<?php get_post_meta( 'phone' ); ?>

<?php }

} else {

// no posts found

}

// Restore original Post Data
wp_reset_postdata();

} ?>


Hariprasad Vijayan comments:

Hi,

Are you getting anything from above code? or it completely wrong?


phxbulldog comments:

It's not returning anything at all.


Hariprasad Vijayan comments:

Oh. okay.
Please run following code and let me know what it displaying

<?php $catargs = array(
'orderby' => 'name',
'order' => 'ASC',
);
$categories = get_categories( $catargs );
foreach ($categories as $category) {?>
<h3><?php echo $category->name; // Category title ?></h3>
<?php } ?>


phxbulldog comments:

I get UNCATEGORIZED. Did you get the PM I sent to you?