WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Post Type not honoring order_by (9 posts)

  1. jduree
    Member
    Posted 1 year ago #

    I am trying to display a custom post type by menu_order, but nothing appears to change even trying to sort by title won't work.

    using
    'query_posts( array(
    'posts_per_page' => 100,
    'post_type' => 'festival',
    'order_by' => 'menu_order',
    'order' => 'ASC',
    ));'
    in my functions:
    'function pitch_festival_init(){
    $labels = array(
    'name' => _x('Festivals', 'post type general name', 'mh'),
    'singular_name' => _x('Festival', 'post type singular name', 'mh'),
    'add_new' => _x('Add New', 'book', 'mh'),
    'add_new_item' => __('Add New Festival', 'mh'),
    'edit_item' => __('Edit Festival', 'mh'),
    'new_item' => __('New Festival', 'mh'),
    'all_items' => __('All Festivals', 'mh'),
    'view_item' => __('View Festival', 'mh'),
    'search_items' => __('Search Festivals', 'mh'),
    'not_found' => __('No festivals found', 'mh'),
    'not_found_in_trash' => __('No festivals found in Trash', 'mh'),
    'parent_item_colon' => '',
    'menu_name' => __('Festivals', 'mh')
    );

    $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => false,
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => fase,
    'menu_position' => null,
    'register_meta_box_cb' => 'mh_festival_metabox_init',
    'supports' => array( 'title', 'thumbnail', 'excerpt','page-attributes', 'editor')
    );'

    any thoughts?

  2. jkrayer
    Member
    Posted 1 year ago #

    I think your problem is 'capability_type' => 'post'

    I believe "Post" Does not use "menu_order." You would need 'capability_type' => 'page' in order to use "menu_order."

    BUT how I read the WP_Query Documentation is that it should work. So possibly you should start by using WP_QUERY instead of "query_posts."

  3. jduree
    Member
    Posted 1 year ago #

    wp_query always give me this error

    Fatal error: Call to undefined function wp_query()

    and 'capability_type' => 'page' appears to make no difference in how the information is parsed....

  4. jkrayer
    Member
    Posted 1 year ago #

    Have you tried:

    $myquery = new WP_Query( array(
    'posts_per_page' => 100,
    'post_type' => 'festival',
    'order_by' => 'menu_order',
    'order' => 'ASC',
    ));

    Then Writing your loop as:
    if ( $myquery->have_posts() ) {
    while ( $myquery->have_posts() ) {
    $myquery->the_post();

    I know this may be elementary but I've never had an issue with WP_Query.

  5. jduree
    Member
    Posted 1 year ago #

    sorry had a brain lapse... I did do that, which got rid of the error, but resulted in no difference in the display of the posts.

  6. jkrayer
    Member
    Posted 1 year ago #

    10-4. The next step I'd take is peeking into the DB, hopefully you have PHP My Admin, and make sure the 'menu_order' values are writing to the db the first place and that they are what you expect them to be.

    If that's a go the next step for me would be to echo the result of the query into the browser and confirm the "order_by" field is part of the result.

    echo "<pre>";
    print_r($myquery);
    echo "</pre>";

    Although now that I think of it if you're using 'capability_type' => 'post' you shouldn't be getting the dialog to enter an order number in the edit screen in the first place.

  7. jduree
    Member
    Posted 1 year ago #

    it is in the db. even tried sorting by a custom meta with no luck. also in the db.

    your query dumps a lot of info

    found this
    [request] => SELECT SQL_CALC_FOUND_ROWS wp_mh_posts.ID FROM wp_mh_posts WHERE 1=1 AND wp_mh_posts.post_type = 'festival' AND (wp_mh_posts.post_status = 'publish' OR wp_mh_posts.post_status = 'private') ORDER BY wp_mh_posts.post_date ASC LIMIT 0, 100

    do you wan the whole dump?

  8. jkrayer
    Member
    Posted 1 year ago #

    It seems you've seen the culprit: "wp_mh_posts.post_date" that is ordering by post_date which is not your desired field.

    Just re-read the above in light of that and I think 'order_by' => 'menu_order' should be 'orderby' => 'menu_order',

  9. jduree
    Member
    Posted 1 year ago #

    weird, that worked!

    '$args = array(
    'posts_per_page' => 100,
    'post_type' => 'festival',
    'orderby' => 'menu_order',
    'order' => 'ASC',
    );
    $the_query = new WP_Query( $args );'

Topic Closed

This topic has been closed to new replies.

About this Topic