WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use the following to retrieve a post from a custom post type and custom taxonomies:

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=blocks&location=Front Page&section=Mainbar');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
    <div class="block-2 border-top">
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php endwhile; ?>

In the Page, I want to create fields that will let the user customize the loop arguments like this:

$custom_posts->query('post_type=blocks&location=<Enter Location>&section=<Enter Section>');

What's the easiest way of doing this?

How would that code look like?

share|improve this question
up vote 1 down vote accepted

the easiest way would be to create a simple form :

<?php $nonce= wp_create_nonce  ('my-nonce'); ?>
<form name="user-query" method="post">
<input type="hidden" name="_wpnonce" id="_wpnonce" value="<?php echo $nonce; ?>"/>
<select name="location" id"location">
  <option value="location1">location1</option>
  <option value="location2">location2</option>
  <option value="location3">location3</option>
  <option value="location4">location4</option>
<select name="section" id"location">
  <option value="section1">section1</option>
  <option value="section2">section2</option>
  <option value="section3">section3</option>
  <option value="section4">section4</option>
<input type="submit" name="create-query" value="create query"/>

and catch that for submit and process

if (isset($_POST['create-query'])){
    if (! wp_verify_nonce($nonce, 'my-nonce') ) die('Security check');
    if (isset($_POST['location']) && isset($_POST['section']) ){
        $args = array(
            'post_type' => 'blocks',
            'location' => $_POST['location'],
            'section' => $_POST['section']

and i would add some security checks but this should get you going.

share|improve this answer
Missing a $ on your nonce var in the second code sample mate.. ;) – t31os Feb 8 '11 at 10:37
@t31os - thanks , updated the code – Bainternet Feb 8 '11 at 10:40
Sorry I'm a bit beginner, Where and how should I add/hook that code? – janoChen Feb 8 '11 at 20:12
the first part is the form where the user selects the location and section. the second code is the processing of the form. – Bainternet Feb 8 '11 at 21:57

Your Answer


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.