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 am currently using this code in my theme right before showing the content of the custom post type my_post_type:

$title = $post->post_title;
query_posts( array (
    'post_type' => 'page', 
    'posts_per_page' => -1,
    'my_taxonomy' => $title
    ));

This means that when I go to .../my_post_type/some_title, I see all pages that are assigned "some_title" as my_taxonomy.

Now I want to do the same thing in a plugin in stead of a theme. This means is (I guess) that I have to use the "request" filter? I have tried to do this:

add_filter( 'request', 'alter_the_query' );
function alter_the_query( $request ) {
    $request['post_type'] = 'page';
    $request['my_taxonomy'] = 'some_title'; // just hardcoded so far while testing
    $request['posts_per_page'] =  -1;
    return $request;
}

But it doen't return any pages at all. Not even this works:

add_filter( 'request', 'alter_the_query' );
function alter_the_query( $request ) {
    $request['post_type'] = 'page';
    return $request;
}

What am I doing wrong, or how can I achieve the same thing as my with my call to query_posts()?

Thanks a lot!

share|improve this question
add comment

2 Answers

WordPress provides a great hook, pre_get_posts for exactly what I think you're trying to do.

Here's what your code would probably look like

add_action( 'pre_get_posts', 'alter_the_query' );
function alter_the_query() {
    if( is_main_query() && get_post_type() == 'my_post_type' ) {
        global $post;
        $title = $post->post_title;
        $query->set('post_type', 'page');
        $query->set('posts_per_page', -1);
        $query->set('my_taxonomy', $title);
    }
}

Nacin and others are recommending that pre_get_posts replace the use of query_posts() anyway, so hopefully this works for you and sends you in the right direction.

share|improve this answer
add comment

The code you've provided doesn't actually print out the list of pages; it just creates the query for them. Assuming you have additional code for displaying them, the easiest way to accomplish what you want in a plugin is to build that list, then add them using the_content filter. I would probably create the list of pages in one function, the add those pages to the_content with a second function added to the filter. Here's something that should help to get your started:

function my_posts_list() {
    global $post;

    query_posts( array (
        'post_type' => 'page', 
        'posts_per_page' => -1,
        'my_taxonomy' => $post->post_title
    ));

    // Build your HTML list of posts from the query.
}

function filter_the_content($content) {
    // Only filter the content if we're on a single 'my_post_type' page.
    if (is_singular('my_post_type')) {
        $postsList = my_posts_list();
        $content = $postsList . $content;
    }
    return $content;
}

add_filter('the_content', 'filter_the_content');

There are, of course, plenty of additional things you could do with this, like check if there the list of posts isn't empty.

share|improve this answer
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.