Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#18858 closed enhancement (wontfix)

Would like to see a function similar to get_objects_in_term that filters by post_type.

Reported by: sqlwiz Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1.4
Component: Taxonomy Keywords: has-patch
Focuses: Cc:


This is a function I have needed on several recent sites when a taxonomy is shared between multiple post types. Without this, one has to check the post_type of all returned objects. I think it would be a good addition to core.

Attachments (1)

get.txt (1.7 KB) - added by sqlwiz 7 years ago.
php source code for proposed function

Download all attachments as: .zip

Change History (7)

7 years ago

php source code for proposed function

#1 @scribu
7 years ago

  • Keywords 2nd-opinion added

Have you tried using WP_Query for this?

#2 @scribu
7 years ago

  • Keywords reporter-feedback added; 2nd-opinion removed

#3 @mikeschinkel
7 years ago

  • Cc mikeschinkel@… added

@sqlqiz - Can you post a couple different example calls to your function that address your use-cases, and the output you'd expect?

#4 @sqlwiz
7 years ago

@mikeschinkel - Hope this isn't too long winded...

You can see an example of the type of problem I'm trying to solve in the standard WP Admin screens. Category, Post Tag and custom taxonomy management pages contain the following fields in the sidebar list table:

Name, Description, Slug and Posts

The Posts field is a hyperlinked count of the total number of posts of all post types, yet if you click on the link, you get only the posts of the current post type.

For example, let's say there are two post types "Posts" and "Interviews" and the post tags are used on both types.

PostPost TypePost Tag

If you look at Post Tags for "Posts", you see:

key key2
important important1

If you click on the "2" for key, you'll see a list of "Posts" type posts, there will be only one post on the list. Worse still is what happens in "Interviews". If you click on the "1" for important, you'll get an empty list. Of course, this could be fixed by replacing the count with the words 'show posts'.

I run into the same issue at times - unwanted object ids returned - when using get_objects_in_term(). I have to loop thru the array and examine each returned object_id with get_post_type. It would be more efficient to deal with this in the DB when hundreds of objects are returned.

A function like get_post_type_objects_in_term would return an array of one object id for the term "key" (assuming $term_id is the id).

get_post_type_objects_in_term($term_id, 'post', 'post_tag')

I am using this with custom post_types and custom taxonomies on a number of sites.

@scribu -

Love tax_query - use it all the time. This is more of a convenience wrapper for themers that are more designers than developers and find wp_query a chore to use over and over.

In fact, we are doing post to post links - using taxomony - creating master-detail relationships and all we want at times is a field from the master or the master's meta. A simple function call is much more maintainable.

I am aware of the post 2 post plugin - and the design change you made from taxonomy to custom table - as well as the ongoing improvements to the plugin. It looks really promising for those using WP as a CMS. We would use it if it became part of core. Until then, we'll try make do.

#5 @scribu
7 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

If this can be achieved via WP_Query, just make a wrapper function that calls WP_Query, specifically tailored for your use-case.

I see no reason to add this to Core.

#6 @scribu
7 years ago

The only reason get_objects_in_term() exists is because terms can be attached to other things besides posts, i.e. links and users.

Note: See TracTickets for help on using tickets.