4

I've created a function to call in a page template, inside of which is the following query:

$custom_news = new WP_Query(array(
     'post_type' => array('post', 'resource', 'job'),
     'posts_per_page' => 5,
     'tag' => 'justice',
     'orderby' => 'post_date',
     'order' => 'DESC',
     )
);

The problem is that the 'resource' and 'job' CPTs are not being included in the query, even though they have been registered with 'taxonomies' => 'post_tag'. Here is the generated SQL:

[request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (13) ) AND wp_posts.post_type IN (‘post’, ‘portfolio’) AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_author = 1 AND wp_posts.post_status = ‘private’) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 5

The only CPTs included in the query are 'post' and 'portfolio' (which is a CPT that comes with the theme I'm using).

However, if I remove the 'tag' => 'justice' argument, the post type argument is "seen" by the query, and the following SQL is generated:

[request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type IN (‘post’, ‘resource’, ‘job’) AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_author = 1 AND wp_posts.post_status = ‘private’) ORDER BY wp_posts.post_date DESC LIMIT 0, 5

I should also mention that I've tried it with the 'tag__in' and tax_query arguments to no avail.

Could anyone illuminate why this might be? It could be I'm missing something very basic, and if so, forgive me.

Thanks, matt

4
  • Have you tested this on a seperate install, ie. on another system(not the one you're having a problem with)..? ... just to rule that out(and don't say yes just for the hell of it... please - i know i'm guilty of that from time to time)..
    – t31os
    Nov 29, 2011 at 23:01
  • t3los -- thanks for taking the time to comment. No, I haven't tested it on a separate install, but I intend to, as it "feels" odd to me why it doesn't work -- unless it's by design. I don't know why the portfolio CPT is 'privileged' in that it is included with 'post' in the taxonomy based query. This is the closest I've come to finding something that relates, but I'm a neophyte with SQL: stackoverflow.com/questions/2646894/…. Perhaps I'll try to cobble something together -- just seems like it should work 'out-of-the-box'.
    – deukalion
    Nov 29, 2011 at 23:13
  • 1
    In any case, part of problem solving is eliminating possbile causes... test another system, if the problem is reprodcued you've at least ruled out a system specific problem..
    – t31os
    Nov 29, 2011 at 23:22
  • close voted as too localized: Too less info (theme name & download).
    – kaiser
    Apr 19, 2012 at 3:30

4 Answers 4

1

I just got the same problem and the problem was exclude_from_search post_type option. It seems that WordPress got a bug (I'm just going to report it), that when you got exclude_from_search false, the posts do not show on tax pages

0
0

It seems that your Theme is modifying the query after you're adding your query. Propably the pre_get_posts filter.

0

The problem is that the 'resource' and 'job' CPTs are not being included in the query, even though they have been registered with 'taxonomies' => 'post_tag'.

The taxonomies argument in register_post_type takes an array not a string.

taxonomies (array) (optional) An array of registered taxonomies like category or post_tag that will be used with this post type. This can be used in lieu of calling register_taxonomy_for_object_type() directly. Custom taxonomies still need to be registered with register_taxonomy(). Default: no taxonomies

It should be 'taxonomies' => array( 'post_tag' )

-1

This will resolve your issue, I was have the same problem, when I typed this it worked perfectly.

$args = array(
     'post_type' => array('post', 'resource', 'job'),
     'posts_per_page' => 5,
     'tag' => 'justice',
     'orderby' => 'post_date',
     'order' => 'DESC',
    );              

$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); 
4
  • 1
    Isn't this essentially a copy/paste of the code in the question? How is that supposed to help? Can you clarify or explain at all?
    – s_ha_dum
    May 29, 2015 at 16:15
  • It's not the array part that was the issue, that's done correctly. The bottom part of the code I shared, $the_query section is what helped make WordPress properly filter the custom post type by tags. Jun 5, 2015 at 15:33
  • You have trivially reorganized the code. It is still the functional equivalent of that in the question.
    – s_ha_dum
    Jun 5, 2015 at 15:36
  • But it works when the one above did not work, isn't that what matters? Jun 5, 2015 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.