Make WordPress Core

Opened 21 years ago

Closed 21 years ago

Last modified 20 years ago

#1479 closed enhancement (fixed)

Add more filters to the query string

Reported by: christined Owned by: skippy
Priority: normal Milestone:
Component: General Version: 1.5.2
Severity: normal Keywords: posts filter bg|has-patch bg|dev-feedback
Cc: Focuses:

Description

I'm wanting to write a posts query for a plugin which needs to use a "having" clause. However, it's not something I seem to be able to wrangle at the moment. The having clause needs to go between the group by and order by clauses; but there doesn't seem to be a way for me to put anything between the two. Putting the having clause either before the group by, or after the order by gives me a SQL error.

The sql that is output at the moment, seems to look like
SELECT * FROM posts {posts_join} WHERE {posts_where} GROUP BY posts.ID ORDER BY (posts_order)

What I am wanting to be able to do, is something along the lines of
SELECT * FROM posts {posts_join} WHERE {posts_where} GROUP BY posts.ID (posts_having) ORDER BY (posts_order)

or

SELECT * FROM posts {posts_join} WHERE {posts_where} GROUP BY posts.ID (posts_group_by) ORDER BY (posts_order)

Cheers,
Christine

Attachments (1)

posts_groupby_paged.diff​ (1.0 KB ) - added by westi 21 years ago.
diff to add a filter for posts_grouped_by

Download all attachments as: .zip

Change History (8)

@westi
21 years ago

diff to add a filter for posts_grouped_by

#1 @westi
21 years ago

Hi Christine,

Try the patch attached for a possible solution. Untested so it may have php errors.

westi

#2 @Denis-de-Bernardy
21 years ago

  • Priority lowest → normal
  • Summary Add a filter for posts_having or posts_group_by → Add more filters to the query string

as a complement to Christine's request, it is mostly useless to group by in an sql statement with a distinct clause, since distinct means group by everything. so what we really want here is two plugin hooks, one for the group by clause, and another one for the select clause, in order to define aggregates.

e.g.

select *,
  case
    when this
      then do_this
    when that
      then do_that
    else do_else
  as my_aggr
...
group by ID, my_aggr
...

Also, the distinct clause is useless, since we're grouping by ID.

And lastly, there is a strong case to name the tables used so as to allow a double join on the posts table, for instance.

In all, it might make sense to to remove all the query filters and to replace them with a the_request hook. that way, plugin devs will only need to work out one query hook, and there will no longer be any incentive to throw a 1=0 where clause to later catch the_posts and do the query 'the right way'.

#3 @skippy
21 years ago

  • Keywords bg|has-patch bg|dev-feedback added
  • Owner changed from anonymous to skippy
  • Status new → assigned

#4 @skippy
21 years ago

  • Milestone → 1.5.2

#5 @matt
21 years ago

  • Milestone 1.5.2 → 1.6

#6 @ryan
21 years ago

  • Resolution → fixed
  • Status assigned → closed

(In [3130]) Add posts_groupby and posts_request filters. fixes #1479

#7 @(none)
20 years ago

  • Milestone 2.0

Milestone 2.0 deleted

Note: See TracTickets for help on using tickets.