• I have these 2 pieces of code that wonderfully do what I like them to do… pull pages and pull posts.

    Now don’t get me wrong, that’s a whole heap-o-pullin’ right there, but I need more pullin’… I’d like to pull both posts and pages in one list, as we all know, 2 is better than 1!

    // Pull all the pages into an array
    $options_pages = array();
    $options_pages_obj = get_pages('sort_column=post_parent,menu_order');
    $options_pages[''] = 'Select a page:';
    foreach ($options_pages_obj as $page) {
    $options_pages[$page->ID] = $page->post_title;
    }
    
    // Pull all the posts into an array
    $options_posts = array();
    $options_posts_obj = get_posts('numberposts=999');
    $options_posts[''] = 'Select a post:';
    foreach ($options_posts_obj as $post) {
    $options_posts[$post->ID] = $post->post_title;
    }

    Thanks πŸ™‚

Viewing 15 replies - 1 through 15 (of 15 total)
  • You’ve lost me as to why you want this, but the following (untested) code to replace the second half of your code should do what you ask:

    // Pull all the posts into the same array
    $options_posts_obj = get_posts('numberposts=999');
    foreach ($options_posts_obj as $post) {
    $options_pages[$post->ID] = $post->post_title;
    }

    Except maybe changing an early line to:
    $options_pages[''] = 'Select a page or post:';

    Thread Starter Pete

    (@perthmetro)

    I think I missed out on some important info (and my attempt at humour was terrible).

    I want to pull both pages and posts in one single list… as it is now these 2 arrays pull either pages or posts in separate lists.

    This how I list the posts

    $options[] = array(
    'name' => __('Tabbers', 'options_check'),
    'desc' => __('Tabber 1?', 'options_check'),
    'id' => 'tabber_post1',
    'type' => 'select',
    'options' => $options_posts);

    and the pages

    $options[] = array(
    'name' => __('Tabbers', 'options_check'),
    'desc' => __('Tabber 1?', 'options_check'),
    'id' => 'tabber_pages1',
    'type' => 'select',
    'options' => $options_pages);

    But I want to list both pages and posts in one list

    thanks πŸ™‚

    Yes, and if you modify the code as I suggest, you would just use the second $options[] = array( statement you list in the last post, as it would put both pages and posts in the same list.

    Thread Starter Pete

    (@perthmetro)

    Ahh got it, thanks so much. How would I prefix the list of titles with their respective type?

    e.g.
    post: hello world!
    page: Sample page

    thanks again πŸ™‚

    Hey,

    Why don’t you try with the below:
    query_posts(‘post_type=page,post’);

    Thanks

    Thread Starter Pete

    (@perthmetro)

    Can you elaborate a bit more (e.g.show me how?)

    How would I prefix the list of titles with their respective type?

    e.g.
    post: hello world!
    page: Sample page

    Change the relevant lines of code as follows:

    $options_pages[$page->ID] = 'page: ' . $page->post_title;
    $options_pages[$post->ID] = 'post: ' . $post->post_title;

    Thread Starter Pete

    (@perthmetro)

    Awesome thanks!

    <?php
    $posts = query_posts(array( ‘post_type’ => array( ‘post’, ‘page’ ) ));

    foreach($posts as $post) {
    echo ‘post type:’, $post->post_type, ‘ post name:’, $post->post_title, ‘
    ‘;
    }
    exit;
    ?>

    Thanks

    Thread Starter Pete

    (@perthmetro)

    Thanks Jon it works well… now I’m trying to use your code like this…

    <?php $recent = new WP_Query('page_id='.of_get_option('test')); while($recent->have_posts()) : $recent->the_post();?>
     <?php the_title(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>

    But it’s only displaying pages not posts

    The of_get_option() function is part of some plugin, and not part of WordPress itself, so I’m not familiar with it. Worse yet, the documentation for it is pretty much non-existent.

    ‘Fraid I can’t help you as what you’ve coded bears no resemblance to anything suggested above.

    All that said, using ‘page_id=’ in WP_Query is obviously only going to return Pages, not Posts.

    Thread Starter Pete

    (@perthmetro)

    is there some function that can output either a page or a post if given an ID? I take it pages and posts don’t have the same ID?

    There are others, but manishkrag gave you an example in his post:
    $posts = query_posts(array( 'post_type' => array( 'post', 'page' ) ));

    Thread Starter Pete

    (@perthmetro)

    I’ll try it out and let you know, thanks πŸ™‚

    On second thought, WP_Query might work (I haven’t tried this) better for your purposes, and is certainly better documented:
    http://codex.wordpress.org/Class_Reference/WP_Query

    I’d experiment with the p= parameter of WP_Query
    http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
    and see if given a Page ID, it will return the Page, not just a Post like the doc. says.

    And, yes, to answer your other question, ID is unique among Pages and Posts.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Combining 2 arrays to fetch posts *and* pages’ is closed to new replies.