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.

We can get multiple posts manually in a page, but that's generated in a template, AFTER the default query returned something else (page/post, etc)

How do you query multiple posts in a public query, which share no taxonomy or anything else?

e.g. by ID:

http://example.com/?p=23,18,2,199,6,8

I got it working already parsing a variable in a page-name.php template, but once it gets the template it's too late to generate e.g. canonicals.

I guess this would be something similar to a search, except the search is a 1-to-multiple-posts relation and this requires a multiple-queries-to-unique-post (each).

Thanks.

share|improve this question

2 Answers 2

Maybe you're looking for the post__in parameter in WP_Query.

$query = new WP_Query(array(
    'post__in' => array(23,18,2,199,6,8)
);

And then:

while ( $query->have_posts() ) {
    $query->the_post();
    /* post loop */
}

Take a look at the docs. =D

For public queries:

post__in is not public queryable by default, so you can just validate and copy $_GET['post__in'] on the parse_query action hook, and let the thing happen.

add_action('parse_query', 'wpse59828_parse_query');
function wpse59828_parse_query($query) {
    if (empty($_GET['post__in']))
        return $query;

    $posts = explode(',', $_GET['post__in']);
    $post__in = array();
    foreach ($posts as $p) {
        $post__in[] = intval($p);
    }

    $query->query_vars['post__in'] = $post__in;
    return $query;
}

Then you would just access this:

http://mywebsite.com/?post__in=23,18,2,199,6,8

Please note that, like this, you won't be able to set the post order in WordPress versions before 3.5 (#13729). Use this plugin if you need to.

share|improve this answer
    
Yes, but I'm trying to get it to work through the PUBLIC query, like mywebsite.com/p=23,18,2,199,6,8, or mywebsite.com/p=post__in=23,18,2,199,6,8, or mywebsite.com/post_parent=12 (this last one would be for pages, and yes, they have something in common, but this one is also defined as "public" and doesn't work either) –  sergio Oct 25 '12 at 5:38
    
@sergio, check the edit. –  vmassuchetto Oct 25 '12 at 14:02
    
Will parse_query hook happen soon enough for the rest of the default functions work accordingly to the queried pages? e.g. everything should match the current query, like multiple posts (archive?) template used, sidebar content and body classes properly for the multiple query? I'm affraid some of the content to be related to something like a "page" while others to the e.g. last post in this query –  sergio Jan 3 '13 at 4:17
    
Hi @sergio. parse_query will run before fetching stuff from database and sending you to the template, so you'll probably have to build your own solution for most of these questions. You can intersect the global $wp_query in this very same function, and then set the template, body class, sidebar and so on... –  vmassuchetto Jan 4 '13 at 11:35
$tax_query = array (
    array (
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => $cat_name
        )
    );

$args = array( 'tax_query' => $tax_query, 'numberposts' => -1);
$lastposts = get_posts( $args );
share|improve this answer
2  
It's cool that you have some code that might slove the OP's problem, but adding a description of exactly what the code does and how it works would be even better –  bungeshea May 8 '13 at 8:36

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.