• I want to include a post in draft status inside another post using a short code.

    I tried the following code to retrieve what I want:

    $post = &query_posts( array(
        'name' => $postslug,
        'post_type' => 'any',
        'post_status' => 'any' ) );

    query_posts finds the right post, then… it deletes it from returned results here.

    The very same function using the post ID works perfectly well:

    $post = &query_posts( array(
        'include' => $postid,
        'post_type' => 'any',
        'post_status' => 'any' ) );

    and returns the post. The ‘include’ parameter accepts IDs, but not slugs.

    In fact, query_posts() assumes single posts and pages must follow access restrictions, while it behaves without any security in mind when asking lists of posts, even if the list is made of one entry.

    How should I proceed to get the post data with its slug only?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter petitnuage

    (@petitnuage)

    It appears I made an error here:

    The very same function using the post ID works perfectly well:

    $post = &query_posts( array(
        'include' => $postid,
        'post_type' => 'any',
        'post_status' => 'any' ) );

    and returns the post. The ‘include’ parameter accepts IDs, but not slugs.

    The working code does not call query_posts() but get_posts():

    $post = &get_posts( array(
        'include' => $postid,
        'post_type' => 'any',
        'post_status' => 'any' ) );

    But that does not change anything to my problem. Any idea would be welcome.

    Hello petitnuage, I unfortunately can’t help you with your current problem. I am not in any sense of the phrase a “code poet” but I was curious if what you are trying to achieve is to get a saved draft to publish? I am currently in that situation. Spent much of my time assembling a post to publish and now it is stranded in Drafts. Could you offer any help?

    Thanks for your consideration,

    Dave

    Thread Starter petitnuage

    (@petitnuage)

    No, I’m not trying to publish a post. I’m working on a WordPress shortcode “insert_another_post” where I want to include any post inside another one on display.

    A post content looks like this:

    blah blah
    
    [insert_another_post id="555"]
    
    blah blah

    That kind of shortcode is usefull to display some short up to date information that has not been written in the intent of becoming a whole independent post or page, just a part of another post.

    What I want is to make also possible to include a post with its slug, in addition to its id:

    blah blah
    
    [insert_another_post name="short-description-cms-wordpress"]
    
    blah blah

    In my case, I can use the id only, but that’s still annoying. I really wish to use the slug to make it more user friendly.

    Now for your problem.

    If you really have to do so, you can ask for all the posts found in all the categories, then loop into the results to find the post you are looking for using its slug.

    It’s a very ineffective solution, and this is the reason I don’t want to use it, but it can be done.

    function get_post_id_from_slug( $slug )
    {
      global $wpdb;
      return $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_name = '".$wpdb->escape( $slug )."' LIMIT 1" );
    }

    then just …

    $post = &get_posts( array(
        'include' => (int) get_post_id_from_slug( $slug ),
        'post_type' => 'any',
        'post_status' => 'any' ) );

    should do it, no?

    Thread Starter petitnuage

    (@petitnuage)

    Thanks George, you’re right, that should do.

    However, that implies to access to the database directly. I’ve been doing that long time ago. Then, years later, the database structure changed. Since then, I prefer to use the WordPress API whenever possible, since I feel it more stable than the database structure. I might be wrong, however.

    What do you think about this?

    I think it’s a fantastic idea to use built-in functions whenever possible, but from a cursory glance I can’t seem to find one to get what you want. I could be mistaken, but for now the db’s solidified to the point where I’d be comfortable doing it this way until a better solutions crops up or it breaks.

    Better to spend 15 mins doing it now, and 15 mins when it breaks in two years, than three hours trying to find something that may not exist. (Half hour searching is fine and good, but don’t wanna go overboard!)

    Thread Starter petitnuage

    (@petitnuage)

    You’re right. I’m going to use your nice function. Thanks a lot! ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to get a post in draft status from its slug?’ is closed to new replies.