• Resolved desirelabs

    (@desirelabs)


    Hello,
    I’m trying to make this ^plugin work with custom post types and custom queries.
    Here are my snippets :

    Custom post type registration :

    <?php
    function create_post_type() {
    	register_post_type('editorial-posts',
    		array(
    			'labels' => array(
    				'name' => __( 'Editorial posts' ),
    				'singular_name' => __( 'Editorial posts' )
    			),
    			'public' => true,
    			'publicly_queryable' => true,
    			'show_ui' => true,
    			'show_in_menu' => true,
    			'query_var' => true,
    			'rewrite' => array(
    				'slug' => 'editorial-posts'
    			),
    			'capability_type' => 'post',
    			'has_archive' => true,
    			'hierarchical' => false,
    			'menu_position' => 4,
    			//'menu_icon' => '',
    			'supports' => array(
    				'title','editor','thumbnail'
    			),
    			'taxonomies' => array('category', 'post_tag')
    		)
    	);
    }
    add_action( 'init', 'create_post_type' );
    ?>

    The query :

    <?php
        // Get the parent cat thanks to the page's cat slug
        $parent_cat = get_category_by_slug( $cat[1] );
        $args = array(
            'type'      => 'post',
            'child_of'  => $parent_cat->term_id
        );
        // child cats
        $categories = get_categories( $args );
        // loop on the child cats to get the sub cats object
        if ($categories) {?>
        <!-- THE TITLE -->
        <div class="text-seperator">
            <h2>Sector's programs</h2>
        </div>
        <ul class="sc_accordion">
        <?php
            foreach ($categories as $key => $value) {
                $args = array(
                    'post_type' => 'editorial-posts',
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'category',
                            'field' => 'slug',
                            'terms' => array( $value->slug ),
                            'include_children' => false
                        ),
                        array(
                            'taxonomy' => 'post_tag',
                            'field' => 'slug',
                            'terms' => array( 'short-description' )
                        )
                    )
                );
                $program_desc_short = new WP_Query( $args );
                /*echo '<pre>';
                var_dump($program_desc_short);
                echo '</pre>';*/
                ?>
                <?php if ($program_desc_short->have_posts()) : $program_desc_short->the_post() ?>
                    <li><a class="sc_accordion-btn" href="#"><?php echo $value->name ?></a>
                    <div class="sc_accordion-content"><?php the_content(); ?>
                    <?php edit_post_link(__('Edit This'),'<p class="edit-link">' ,'</p>'); ?>
                    <a href="<?php echo '/sectors/'.$value->slug ?>" class="btn small-btn right">Read more</a>
                    </div>
                <?php endif;
                wp_reset_query();
            }
        ?>
        </ul>
        <?php
        }
    ?>

    I checked out that I was using hierarchical->false, but still can’t re-order posts.

    Can you help please ?

    Thansk

    https://ww.wp.xz.cn/plugins/post-types-order/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Maya

    (@tdgu)

    Is the autosort turned On? Or you should include ‘orderby’ => ‘menu_order’ within your $args

    Thread Starter desirelabs

    (@desirelabs)

    I tried deactivating auto sort and menu_order. Even other sorting doesn’t work.

    Thread Starter desirelabs

    (@desirelabs)

    Ok, the problem came from the fact I was looping on categories to get the matching post. What I had to do was to get the desired categories and use it in my $args + some adjustments.

    <?php
            // Get the parent cat thanks to the page's cat slug
            $parent_cat = get_category_by_slug( $cat[1] );
            $cats_args = array(
                'type'      => 'post',
                'child_of'  => $parent_cat->term_id
            );
            // child cats
            $categories = get_categories( $cats_args );
            // loop on the child cats to get the sub cats object
            if ($categories) {
                // empty array to store cats
                $cats = array();
                // Get the cat slugs and store it in an array
                foreach ($categories as $key => $value)
                    $cats[] = $value->slug;
                ?>
            <!-- THE TITLE -->
            <div class="text-seperator">
                <h2>Sector's programs</h2>
            </div>
            <ul class="sc_accordion">
            <?php
                $args = array(
                    'post_type' => 'editorial-posts',
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'category',
                            'field' => 'slug',
                            'terms' => $cats,
                            'include_children' => false
                        ),
                        array(
                            'taxonomy' => 'post_tag',
                            'field' => 'slug',
                            'terms' => array( 'short-description' )
                        )
                    )
                );
                $program_desc_short = new WP_Query( $args );
                ?>
                <?php if ($program_desc_short->have_posts()): while ($program_desc_short->have_posts()) : $program_desc_short->the_post() ?>
                    <?php
                    $terms = get_the_terms( get_the_ID(), 'category' );
                    $attached_category = array();
                    foreach ($terms as $term)
                        // in order to only get the concerned category, we check that the term is matching the cats array
                        if (in_array($term->slug, $cats))
                            foreach ($term as $key => $value)
                                $attached_category[$key] = $value;
                    ?>
                    <li><a class="sc_accordion-btn" href="#"><?php echo $attached_category['name'] ?></a>
                    <div class="sc_accordion-content">
                        <?php the_content(); ?>
                        <?php edit_post_link(__('Edit This'),'<p class="edit-link">' ,'</p>'); ?>
                        <a href="<?php echo '/sectors/'.$attached_category['slug'] ?>" class="btn small-btn right">Read more</a>
                    </div>
                <?php
                endwhile;
                endif;
                wp_reset_query();
            ?>
            </ul>
            <?php
            }
        ?>

    Hi! I could really do with some help on a similar query!

    I want to be able to use this ‘post types order’ plugin with ‘Woocommerce extra product sorting plugin as the client wants to be able to initially have the products in the front end in his order thats he sets in the backend, but the customers can also chose to see the items alphabetically.

    Nothing seems to be working properly at the minute.

    I’ve unchecked the AutoSort button in the settings of post types order but I don’t under stand what it means when it says

    “If you need more order customizations you will need to uncheck this and include ‘menu_order’ into your theme queries.”

    Any help appreciated.

    thanks @tdgu

    your tip solved my issue; for this kind of query, used within a shortcode:
    $args = array('post__in' => $atts, 'post_type' => 'my-custom-post-type', 'posts_per_page' => -1);
    post type order showed up the re-order menu but didn’t work at all.
    Including ‘orderby’ => ‘menu_order’ solved:

    $args = array('post__in' => $atts, 'post_type' => 'my-custom-post-type', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC');

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Doesn't work with custom queries ?’ is closed to new replies.