Search code examples
wordpresstaxonomy

how build a wordpress query_posts based on url


I want to use the query_posts function from wordpress for displaying the right posts. I want to use the url www.site.com/?s=taxonomy1=test1&taxonomy2=test2 for building the query. something like down here:

        $taxonomy1 = $_GET['taxonomy1'];
        $taxonomy2 = $_GET['taxonomy2'];

        query_posts(array(
            'posts_per_page' => 10,
            'taxonomy1' => $taxonomy1,
            'taxonomy2' => $taxonomy2,
        ) );>

How i do this?


Solution

  • the wordpress codex is your best friend when trying to build custom queries. something like this should work as a query

    $taxonomy1 = $_GET['taxonomy1'];
    $taxonomy2 = $_GET['taxonomy2'];
    
    $the_query = new WP_Query(array(
      'posts_per_page' => 10,
      'tax_query' => array(
         'relation' => 'AND',
         array(
           'taxonomy' => $taxonomy1,
           'field' => 'slug',
         ),
         array(
           'taxonomy' => $taxonomy2,
           'field' => 'slug',
         ),
       ),  
    ));
    

    and to display the results

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
      //the_title(), the_content(), etc
    endwhile;
    

    note that the query is using the new (as of 3.1) method for querying taxonomies. As you can see it gives you a lot more flexibility and i would suggest reading through the above link