Make WordPress Core

Opened 12 years ago

Closed 10 years ago

Last modified 10 years ago

#15995 closed defect (bug) (invalid)

query is separating post_type 'post' from custom post_types when retrieving custom field

Reported by: bowstreet's profile bowstreet Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.3
Component: Posts, Post Types Keywords: close
Focuses: Cc:

Description

query_posts( 
	array( 'post_type' => array(
		'post',
		'custom_1',
		'custom_2'
		),
	'orderby' => 'title',
	'order'=>'ASC' )
	);

It doesn't matter which parameter you take for 'orderby' it will separate the post_type 'post' from the custom post_types as shown in the example below:

custom_1, title = AAA
custom_2, title = DDD
custom_1, title = GGG
post, title = BBB
post, title = EEE

I cannot believe that this is intended. The proper output should to be:

custom_1, title = AAA
post, title = BBB
custom_2, title = DDD
post, title = EEE
custom_1, title = GGG

Attachments (3)

output.html (11.1 KB) - added by bowstreet 12 years ago.
stickybug_theme.zip (152.5 KB) - added by bowstreet 12 years ago.
stickybug_theme-meta_value-sticky.png (331.8 KB) - added by theaussiepea 10 years ago.

Download all attachments as: .zip

Change History (15)

#1 follow-up: @dd32
12 years ago

  • Keywords reporter-feedback added; query sort post_type custom post removed

This is working as I'd expect..

post - AAA
note - BBB
post - CCC

Can you add var_dump($wp_query); after your query_posts call, save the output to a file and attach it here? It'll help understand how/why it may be occurring.

#2 in reply to: ↑ 1 @bowstreet
12 years ago

Seems it has to do with meta_key and meta_value I'm using.

Last edited 12 years ago by bowstreet (previous) (diff)

#3 @bowstreet
12 years ago

I first forgot to test if it has to do with the meta_key and meta_value in the query. As soon as you call the posts with proper key and value it will sort like I pointed out above.

I used this for the var_dump:

	$feature_query = new WP_Query(
		array(
            'post_type' => array(
                'post',
                'issues',
                'videos',
            ),
		'meta_key' => 'sticky',
		'meta_compare' => '=',
		'meta_value' => 'yes',
		'orderby' => 'modified',
		'order'=>'ASC' )
        );
		
		var_dump($feature_query);

        if ( $feature_query->have_posts() ):
            while( $feature_query->have_posts() ) : $feature_query->the_post();

@bowstreet
12 years ago

#4 follow-up: @bowstreet
12 years ago

  • Summary changed from query is separating post_type 'post' and from custom post_types to query is separating post_type 'post' from custom post_types when retrieving custom field

#5 in reply to: ↑ 4 @bowstreet
12 years ago

Can someone confirm this?

#6 follow-up: @dd32
12 years ago

Thanks for the follow up,

I've re-tested using meta as well, and ordering by title (as the initial ticket says) and ordering by modified (as your code is doing) is working correctly for me.

I've tried marking some of the posts as sticky, and that's not affecting the results either.

Can you try executing the SQL query directly and see if the ordered results are correct?
The SQL in your case is:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)  WHERE 1=1  AND wp_posts.post_type IN ('post', 'issues', 'videos') AND (wp_posts.post_status = 'publish') AND wp_postmeta.meta_key = 'sticky' AND wp_postmeta.meta_value = 'yes'  GROUP BY wp_posts.ID ORDER BY wp_posts.post_modified ASC LIMIT 0, 10

#7 in reply to: ↑ 6 @bowstreet
12 years ago

Thank you, I'm still checking back on this for hours now. The SQL query always brings the ordered result correctly but the query doesn't. I must admit that the query is giving the proper output when just using the register_post_type and adding a custom field in the backend with the name 'sticky' and the value 'yes' but as soon as you add a meta_box for the meta_key sticky it will really make it 'sticky'. Means that you can see the little "Sticky" mark in the overview of the post_type. I don't know the difference between making a custom field with the name sticky or adding a meta_box with the name sticky. It looks like the tiny word 'sticky' was making all the trouble.

The code below should help reproducing the bug.

add_action('init', 'issue_custom_init');
function issue_custom_init() 
{
  $args = array(
	'label' => __('Issues'),
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail','excerpt','comments','revisions','custom-fields')
  ); 
  register_post_type('issues',$args);
}

add_action("admin_init", "admin_init");
 
function admin_init(){
  add_meta_box("sticky", "Sticky", "sticky", "issues", "side", "low");
}

function sticky(){
  global $post;
  $sticky = get_post_meta($post->ID, 'sticky', true);

  echo '<input type="checkbox" id="sticky" name="sticky" value="yes"';
  echo ($sticky == 'yes') ? 'checked="checked"':'' .'/> ';
  echo '<label for="sticky">Check to stick to the frontpage.</label>';
}

add_action("save_post", "save_details");

function save_details($post_id) {
     global $post;

     if(isset($_POST['post_type']) && ($_POST['post_type'] == "issues")) {
		update_post_meta($post->ID, "sticky", $_POST["sticky"]);
     }
}

#8 follow-up: @dd32
12 years ago

Can you try clearing your Sticky posts option? (option name is 'sticky_posts') Alternatively, Can you reproduce it on a clean installation?

It could be related to #12702 somehow.

I did a quick test with your code, and didn't run into an error, but, didn't test thoroughly with different orderings.

#9 in reply to: ↑ 8 @bowstreet
12 years ago

Yes I reproduced this on three clean installations each of them on a different environment/server.

I made a theme including three files, index.php having three different queries, the functions.php which adds a post_type "custom" with a meta_box called "sticky" and the necessary style.css.

Well lets reproduce this step by step. First set up a clean Wordpress, make a new folder in your themes folder, upload the three files into the new folder.

Log into backend as admin and add a "Post" with title "AAA" make it sticky via "Visibility: Public, Sticky", add another "Post" with title "CCC" make it sticky the same way. (yes there is already shown the newly added meta_box from functions.php causing another bug as I just found out, let's do this later)

Now go to the "Custom" post_type and add two new pages with the title "BBB" and "DDD", make both of them "sticky" with the custom meta_box on the right side.

In the list/overview of "Custom" it will now mark the "BBB" and "DDD" pages as "sticky".

The index.php output now should now be the same as shown in the enclosed screenshot.

Try playing around by enabling and disabling the sticky checkbox to see what happens with the output queries.

When checking the "sticky" meta_box in a "post" you will soon get scrambled egg.

Let me know if it's "working fine". I added the theme as a attachment.

#10 @nacin
12 years ago

  • Keywords dev-feedback added; reporter-feedback removed

#11 @theaussiepea
10 years ago

  • Cc theaussiepea added
  • Keywords close added; dev-feedback removed
  • Resolution set to invalid
  • Status changed from new to closed

Tested this at Contributor Day 2013:
Sticky posts currently use the following format in the postmeta table:

meta_key => 'sticky',
meta_value => 'sticky',

If the provided files in stickybug_theme.zip are modified to reflect this, but in saving the post meta and in the meta query arguments, the posts sort correctly. (See attached screenshot)

#12 @helen
10 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.