0

The behavior of the below should be that the user makes a selection on the dropdown box and the page refreshes with a query arg of editevent=1 and also passes a value of the post ID with the selection chosen. Above the dropdown, it should say

Post ID: [post ID here]

The problem is with this bit of code:

<option value="<?php the_ID(); ?>"><?php the_title(); ?>
</option>

When I put the ID of the post as the value, it makes the form redirect to the actual post instead of just refreshing the current page. I can't figure out why it's doing that. I have the form set to action="#" and there is no reference to the permalink of any URL in the code.

Here is the full snippet:

<?php if ($_GET['editevent'] == true) {
    echo 'Post ID: '.$_GET['page_id'];
}
?>
    <form method="GET" action="#">
    <select name="page_id" id="page_id" onchange="this.form.submit()">
        <?php
        global $post;
        $args = array( 'numberposts' => -1, 'post_type' => 'event', 'season' => '2013-2014');
        $posts = get_posts($args); 
        foreach( $posts as $post ) : setup_postdata($post); ?>
            <option value="<?php the_ID(); ?>"><?php the_title(); ?>
            </option>
        <?php endforeach; ?>
    </select>
    <input type="hidden" name="editevent" value="true" />
</form>
1

Rename your <select> to something other than page_id.

The problem is that page_id is already taken (and handled) by WordPress (as query var).

This should do, for example:

<?php
if (isset($_GET['editevent']) && true == $_GET['editevent'])
    echo 'Post ID: '.$_GET['my_page_id'];
?>
<form method="GET" action="#">
    <select name="my_page_id" id="my_page_id" onchange="this.form.submit()">
        <?php
        $args = array(
            'posts_per_page' => -1,
            'post_type' => 'event',
            'season' => '2013-2014',
        );
        $posts = get_posts($args); 
        foreach ($posts as $post) :
            setup_postdata($post);
            ?>
            <option value="<?php the_ID(); ?>"><?php the_title(); ?></option>
            <?php
        endforeach;
        ?>
    </select>
    <input type="hidden" name="editevent" value="true" />
</form>
2
  • THANK YOU. Ugh, I hate when it's something that dumb and I spent an hour trying to figure it out! – Eckstein Oct 7 '13 at 18:51
  • 2
    Glad I could help. Some smartass tip for the future: When something isn't behaving as expected, first of all assume it's working correctly, anyway. Then try to understand why it's working correctly - while not doing what you want it to. Oftentimes you could have spent only a little time trying to find your error in reasoning, if you hadn't been busy trying to find the mistake on the other side. ;) Would you mind accepting my answer to give this question some peace? – tfrommen Oct 7 '13 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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