Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort on meta field #15

Open
perifer opened this issue Jan 26, 2017 · 7 comments
Open

Sort on meta field #15

perifer opened this issue Jan 26, 2017 · 7 comments

Comments

@perifer
Copy link
Contributor

perifer commented Jan 26, 2017

I continue to have issues when trying to sort on a meta field in the posts module. This is what I have found so far:

Sorting on fields except meta fields seems to work.
Sorting on any meta fields seems to use post_date.

This is what I get with wp debug bar with define('SAVEQUERIES', true); set when sorting on a meta filed called event_place:

SELECT posts.* FROM posts LEFT JOIN postmeta ON ( posts.ID = postmeta.post_id ) LEFT JOIN postmeta AS mt1 ON (posts.ID = mt1.post_id AND mt1.meta_key = 'event_place' ) WHERE 1=1 AND ( 
postmeta.meta_key = 'event_place' 
OR 
mt1.post_id IS NULL
) AND posts.post_type = 'evenemang' AND ((posts.post_status = 'publish')) GROUP BY posts.ID ORDER BY posts.post_date DESC

Notice that it seems to know about event_place but still sorts on post_date. This is what var_dump($getPostsArgs); gives in Posts.php:

array (size=5)
  'posts_per_page' => string '-1' (length=2)
  'post_type' => string 'evenemang' (length=9)
  'order' => string 'desc' (length=4)
  'orderby' => string '_metakey_event_place' (length=20)
  'meta_query' => 
    array (size=3)
      'relation' => string 'OR' (length=2)
      0 => 
        array (size=2)
          'key' => string 'event_place' (length=11)
          'compare' => string 'EXISTS' (length=6)
      1 => 
        array (size=2)
          'key' => string 'event_place' (length=11)
          'compare' => string 'NOT EXISTS' (length=10)

It might not be related but I think it might be something similair on the archive page which also have a settings to sort on meta fields.

@perifer
Copy link
Contributor Author

perifer commented Jan 27, 2017

I created two pull request for this issue:

helsingborg-stad/Municipio#18
#16

@perifer
Copy link
Contributor Author

perifer commented Feb 1, 2017

helsingborg-stad/Municipio#18 is merged. I think the meta_query might be behave differently with posts that don't have the meta field, compared to using the simpler

$query->set('orderby', 'meta_value');	
$query->set('meta_key', 'start_date');

I think the simpler approach might exclude posts that don't have the meta field. Needs testing though. And not sure if it's a problem with this use-case.

@perifer
Copy link
Contributor Author

perifer commented Mar 8, 2017

Is there any reason this hasn't been merged? This should be the same approach as in helsingborg-stad/Municipio#18 (which is merged).

@sebastianthulin
Copy link
Member

This has not been merged yet due to lack of documentation about the solution. What are the effects of the change and why does i work?

@perifer
Copy link
Contributor Author

perifer commented Mar 15, 2017

Are we talking about #16 now?

That PR makes it possible to use meta field in e.g the post module for sorting posts. This does not work without this fix.

@perifer
Copy link
Contributor Author

perifer commented Mar 15, 2017

Also see #16 (comment)

@perifer
Copy link
Contributor Author

perifer commented May 29, 2017

FYI I've currently postsponed work on this since I don't think we will need this fix when we are using your new event solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants