WordPress.org

Forums

Trouble with custom post types and query (1 post)

  1. lynnema
    Member
    Posted 1 year ago #

    Hi all

    I'm 90% to where I want to be. The page in question is here:

    So far, I've queried my custom post type, and have it listing by parent category. I want it to list alphabetically. The problem I'm having is that it is sorting within the parent by child category... So it's not a fully alphabetical list.

    Here is the code I'm using...

    <?php
    
        $args = array(
            'orderby'       => 'name',
    		'parent'        => 0,
            'order'         => 'ASC',
            'hide_empty'    => 1,
            'taxonomy'      => 'category', //change this to any taxonomy
        );
        foreach (get_categories($args) as $tax) :
            $args = array(
                'post_type'         => 'dirlisting', //change to your post_type
                'posts_per_page'    => -1,
                'orderby'           => 'name',
                'orderby'           => 'ASC',
                'tax_query' => array(
                    array(
                        'taxonomy'  => 'category', //change this to any taxonomy
                        'field'     => 'name',
                        'terms'     => $tax->slug,
    					'include_children' => false
                    )
                )
            );
            if (get_posts($args)) :
        ?>
    
            <h2><?php echo $tax->name; ?></h2>
            <ul>
                <?php foreach(get_posts($args) as $p) : ?>
                    <li><a href="<?php echo get_permalink($p); ?>"><?php echo $p->post_title; ?></a></li>
                <?php endforeach; ?>
            </ul>
        <?php
            endif;
        endforeach;
    ?>

    Any help would be greatly appreciated. I just want everything in the parent category to be alphabetical, regardless of child.

    TIA!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.