1

I'm trying to integrate a slider custom post type into WordPress and I can't seem to set the order (orderby:ID, order:ASC) for that custom post type.

I've added the supports => array('title','page-attributes'), to the post type, but when I change the order attribute for a slide, it doesn't change it's order (neither on the back-end or on the front-end).

This is the functions.php (it also give this error: Notice: Undefined index: post_type in .../functions.php on line 105)

function set_custom_post_types_admin_order($wp_query) {
$post_type = $wp_query->query['post_type']; // line 105
    if ( $post_type == 'bxslider') {
      $wp_query->set('orderby', 'ID');
      $wp_query->set('order', 'ASC');
    }
}
add_filter('pre_get_posts', 'set_custom_post_types_admin_order');

And this is the bxslider.php

<?php 

// Create Slider

function hostify_bxslider_template() {

    // Query Arguments
    $args = array(
        'post_type' => 'bxslider',
        'orderby' => 'ID',
        'order' => 'ASC',
        'posts_per_page' => -1
    );  

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

    // Check if the Query returns any posts
    if ( $the_query->have_posts() ) {

        // Start the Slider ?>
        <div id="slider">

            <?php
            while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <div>
                    <div class="slide">

                    <?php // Check if there's a BxSlider h2 given and if so post it
                    if ( get_post_meta( get_the_id(), 'bxslide_metabox_h2', true) != '' ) { ?>
                        <h2><?php echo get_post_meta( get_the_id(), 'bxslide_metabox_h2', true); ?></h2>
                    <?php } ?>

                    <?php // Check if there's a BxSlider h1 given and if so post it
                    if ( get_post_meta( get_the_id(), 'bxslide_metabox_h1', true) != '' ) { ?>
                        <h1><?php echo get_post_meta( get_the_id(), 'bxslide_metabox_h1', true); ?></h1>
                    <?php } ?>

                    <?php // Check if there's a BxSlider paragraph given and if so post it
                    if ( get_post_meta( get_the_id(), 'bxslide_metabox_text', true) != '' ) { ?>
                        <p><?php echo get_post_meta( get_the_id(), 'bxslide_metabox_text', true); ?></p>
                    <?php }

                    // Check if there's a BxSlider button link or button name given and if so post it
                    if ( get_post_meta( get_the_id(), 'bxslide_metabox_link', true) != '' || get_post_meta( get_the_id(), 'bxslide_metabox_name', true) != '' ) { ?>
                        <a href="<?php echo esc_url( get_post_meta( get_the_id(), 'bxslide_metabox_link', true) ); ?>" class="button2"><?php echo get_post_meta( get_the_id(), 'bxslide_metabox_name', true); ?></a>
                    <?php } ?>
                        <div class="clear"></div><br />
                    </div>
                </div>
            <?php endwhile; } ?>
        </div>

        <?php 

    // Reset Post Data
    wp_reset_postdata();
}

?>

P.S. The slider works just fine, I just can't change the order of the slides.

1
  • pre_get_posts is an action hook, not a filter hook; try add_action( 'pre_get_posts', 'my_function' ); Also, $query->query_vars['post_type'] looks to be where you'd find your post_type.
    – Pat J
    Commented Sep 3, 2014 at 13:08

1 Answer 1

1

As @Pat said, pre_get_posts is action hook, it wont work that way. But you don't need to use pre_get_posts hook in functions.php. Remove this functions and filter from functions file and use these arguments in you WP_Query. orderby instead of ID, use menu_order.

$args = array(
    'post_type' => 'bxslider',
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'posts_per_page' => -1,
    'ignore_sticky_posts' => 1,
);
2
  • Using 'menu_order' instead of "ID" solved the problem. Thanks to both of you. Commented Sep 3, 2014 at 13:29
  • FWIW, add_action is just a wrapper around add_filter (they both use the same underlying API), and both will work in this case - but you should still use add_action here ;) Commented Sep 3, 2014 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.