Support » Fixing WordPress » Display specific posts on pages

  • So I’m having a problem.
    In the website every product is a post, but when we add new products we want something like a newsletter, mostly like a post so in the sidebar of the home page you can see the new products or events of the month.
    I’m using pages because I don’t want to re-post a product on every new newsletter so I junt wanna display the posts inside the page.
    In the products page I separate every product by category and sub-category but since I want to group specific post to publish them on the sidebar I think that pages was the best way to do it.

    Right now I’m using this code:

    $productos = new WP_Query(array(
    'post__in'=> array(81, 83),
    ); if ($productos->have_posts()) : while ($productos->have_posts()) : $productos->the_post();

    It display the posts with the id of 81 and 83, I would like to show post by slug using ‘name’ as the codex says because is going to take some time to be checking the ids of the new post, insted of using the name of every new product but It dosen’t work in array or I’m doing something wrong.

    Now I will love to make something like this work

    $names = get_post_meta($post->ID, "names", $single = true); 
    $productos = new WP_Query(array(
    'name'=> array($names),

    So every time I publish a new page I just write the slugs of the post I want to include in the page, as you can see I’m not very good with php but I trying to learn and I search a lot for something that could work before asking in here.

    I try the ggis inline post plugin and although it works I need the id for every post I want to include and I will need to edit the plugin because I want a different order in the output of the post thats why I don’t like to depend to much on plugins.

    I hope someone can help me with this and sorry for my bad english (not my first language)

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can get the IDs in an array by using this:

    $name_string = get_post_meta($post->ID,'names',true);
    $names_quoted = implode(',', array_map(
          function($name) { return "'" . trim($name) . "'"; },
    $sql = "SELECT ID FROM $wpdb->posts WHERE post_name IN ($names_quoted) AND post_status = 'publish' and post_date <= NOW()";
    $ids = $wpdb->get_col($sql);

    Then use $ids in the post__in parameter.

    I forgot to mention that the slugs should be stored in a comma-separated list in the ‘names’ Custom Field.

    Wow that is great vtxyzzy. Yesterday I find a workaround using Shortcodes and it adds more flexibility but I’m going to save that code for another time. Thank you!

    So I put this on my functions.php page

    function productos($atts, $content = null) {
            "slug" => '',
            "query" => ''
        ), $atts));
        global $wp_query,$post;
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query(array(
        'name'=> $slug,
            $query .= '&name='.$slug;
            $query .= $query;
        <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
            <h1><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
            <div><?php the_content() ?></div>
        <?php endwhile; ?>
        <?php $wp_query = null; $wp_query = $temp;
        $content = ob_get_contents();
        return $content;
    add_shortcode("producto", "productos");

    And in my page template I just write [producto slug=”MY-SLUG”] and that way I can display multiple post just with the slugs. Hope someone find this useful.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display specific posts on pages’ is closed to new replies.