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.

In the Admin section I'm using quesry posts to get the title and content of a custom post

this query_posts('post_type=sfnewsletter'); will get me the content I need but unfortunately what I need to do now is to pass the ID of the custom post via a form and then retrieve that content

I've tried

query_posts('p=32');  
query_posts('page_id=32');  
query_posts('page=32');  
query_posts('sfnewsletter_id=32');  

And not only do I not get the right custom post some of them return a post with an ID of 14 (not the first or last post)

I have confirmed that 32 is the correct ID for the custom post I am trying to get.

THIS IS THE FORM

It passes the ID correctly

        <form method="post" action="<?php echo sfn_form_url(); ?>" >

            <table class="form-table">

                <tr valign="top"><th scope="row"><?php _e( 'To', 'sfnewsletter' ); ?></th>
                    <td>
                        <select name="sfnewsletter_to" id="sfnewsletter_to">
                        <option value="0">Please Choose</option>
                        <?php wp_dropdown_roles() ?>
                        </select>
                    </td>
                </tr>

                <tr valign="top"><th scope="row"><?php _e( 'Newsletter', 'sfnewsletter' ); ?></th>
                    <td>
                        <select name="sfnewsletter_newsletter" id="sfnewsletter_newsletter">
                        <option value="0">Please Choose</option>
<?php 
                        query_posts('post_type=sfnewsletter'); 
                        if (have_posts()) : while (have_posts()) : the_post();

                            echo '<option value="' .get_the_ID(). '">' .get_the_title(). '</option>';

                        endwhile; endif;

?>
                        </select>
                    </td>
                </tr>

            </table>

            <p class="submit">
                <input id="sfnewsletter_hidden" class="regular-text" type="hidden" name="sfnewsletter_hidden" value="<?php esc_attr_e( 'SEND' ); ?>" />
                <input type="submit" class="button-primary" value="<?php _e( 'Send Message', 'sfnewsletter' ); ?>" />
            </p>
        </form>

THIS IS THE SOLUTION I CAME UP WITH. THE ANSWER I HAVE ACCEPTED THE RIGHT WAY TO DO IT THOUGH, MY WAY IS A FUDGE


ok, I have come up with a work around that suits my situation.

Since I don't seem to be able to use my custom post ID in the query I have used the ID from the form and converted it into the permalink and then used that in the query like so

    $sfnewsletter_slug = basename( get_permalink($sfnewsletter_newsletter) );

    $the_query = new WP_Query( array( 'post_type' =>'sfnewsletter', 'posts_per_page'=>1, 'name'=>$sfnewsletter_slug ) );

This gives me the output I am looking for.


share|improve this question
    
You should post an example of your form. –  kaiser May 12 '11 at 11:08
1  
ok, added the code for the form –  Andrew Benbow May 12 '11 at 11:35
1  
ok, I have answered my own question but can't post the answer for about 6 hours. I'm using the permalink to get the results I need. I'll be back in 7 hours to post my query –  Andrew Benbow May 12 '11 at 12:34

2 Answers 2

up vote 1 down vote accepted
query_posts('post_type=sfnewsletter&p=32');
share|improve this answer
    
ok, that worked, thank you. Better than my answer too –  Andrew Benbow May 12 '11 at 17:43

The example queries you've posted are for Pages, not custom post types. "p" is for "post" post type, "page_id" and "page" are for "page" post types.

Your query should be:

query_posts(array('post_type' => 'sfnewsletter'));

That will grab he newsletter posts. If you want a specific ID, then within that query, you just need to ask for

query_posts(array('post_type' => 'sfnewsletter'));
if(have_posts()) : while(have_posts()) : the_post(); 
$postid = $post->ID; // retrieves the post IDs within the Loop.
share|improve this answer
    
Hi Shelly, I have tried p and page_id to no avail. As I said I've found a solution that allows me to get the one custom post I need rather than looping through all of the custom posts until I get to the right ID (which seems very inefficient IMHO). Unfortunately I can't post my answer for another 5 hours because of Stack Exchange restrictions –  Andrew Benbow May 12 '11 at 14:27
    
Yes, but what I was saying was "p" and "page" (in the query) are for standard post types. In your above examples, you were trying to pull ID's from custom post types. That's why it wasn't working - that's all I was pointing out with that. I do so want to see your solution though! :) –  Shelly May 12 '11 at 14:32
    
ah, I see, sorry I misread your comment. TBH I'm surprised we can't just use ID to get what ever we want, I'm sure there's a good reason though –  Andrew Benbow May 12 '11 at 17:32
    
I've updated my original question with the solution I had cobbled together, Milo's solution is the correct way though, at least IMHO –  Andrew Benbow May 12 '11 at 21:28

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.