Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm new to the "posts_" filter hooks and wanted to know a few things from those in the know:

  • In this question, someone posted an answer using posts_join that took a second parameter of $query:

    add_filter('posts_join',array(&$this,'posts_join'),10,2);

    ...

    function posts_join($join,$query) {

    }

    Is this an instance of wp_query or something similar?

  • -Same example: how would I determine the post type so that I can do custom joins for each custom post type I have on the admin side

  • -What does the posts_fields filter hook do? from the example I've seen, it looks like it replaces the columns in the SELECT clause of an SQL call. Am I correct in that assumption? And does it too have more parameters that can be called??

I find some examples but I can't get any solid documentation anywhere. Thank you in advance for all answers given

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

When you use one of the methods to query for posts (query_posts(), get_posts() or WP_Query object) arguments you provide are processed and turned into SQL query. This happens in WP_Query->&get_posts() method.

Since arguments are not omnipotent there are a lot of hooks in there that allow to modify or override parts of resulting SQL query.

  • posts_join is part of query that handles SQL JOINs - adding additional tables to the mix, for example tables related to taxonomies when they are needed.

  • posts_fields seem to control which database fields will be returned in query, it seems to default to all fields from posts table.

share|improve this answer
    
So basically post_fields replaces the select part of a query. Thank you for the response. –  Manny Fleurmond Dec 31 '10 at 20:02
    
Not all of SELECT, it looks like this in code SELECT $found_rows $distinct $fields FROM where $fields variable is passed through posts_fields filter. –  Rarst Dec 31 '10 at 20:09
    
So, if I were to use the posts_fields filter, how would I access the results?? Is there a way to get to the from thw WP_query class? –  Manny Fleurmond Jan 2 '11 at 4:29
    
@Manny Fleurmond if I am not mistaken WP_Query->query() method returns the array of posts you queried for, affected by this filter and all others. They should also be saved in WP_Query->posts field after that. –  Rarst Jan 2 '11 at 17:12
    
Note that get_posts() defaults to suppressing filters, while query_posts() does not: get_posts() > parameters. –  Alexander Wallin Jan 23 '13 at 1:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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