WordPress.org

Ready to get started?Download WordPress

Forums

Page array not working (11 posts)

  1. greencode
    Member
    Posted 1 year ago #

    I'm using this query on a custom page

    <?php
    $args = array(
      'post_type' => 'page',
      'post__in' => array(get_post_meta($post->ID, "page_id", true))
      );
    query_posts($args);
    ?>

    So I'm adding the page ids I want to call in a custom field called "page_id"

    So for example in that field I have 220,221,222 but only the first page is getting called.

    Any ideas?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try just using get_post_meta($post->ID, "page_id")

    If only $id is set it will return all meta values in an associative array.
    If $single is set to false, or left blank, the function returns an array containing all values of the specified key.
    If $single is set to true, the function returns the first value of the specified key (not in an array)

    http://codex.wordpress.org/Function_Reference/get_post_meta

  3. greencode
    Member
    Posted 1 year ago #

    Umm, I tried this instead

    <?php
    $args = array(
      'post_type' => 'page',
      'post__in' => get_post_meta($post->ID, "page_id")
      );
    query_posts($args);
    ?>

    But still only one page displays.

    I then tried

    <?php
    $args = array(
      'post_type' => 'page',
      'post__in' => array(get_post_meta($post->ID, "page_id", false))
      );
    query_posts($args);
    ?>

    and nothing displays.

    I tried this, just to make sure it was working when not using the custom field value

    <?php
    $args = array(
      'post_type' => 'page',
      'post__in' => array(361,363)
      );
    query_posts($args);
    ?>

    and it displays okay

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try:

    <?php
    $not_pages = get_post_meta($post->ID, "page_id");
    print_r($not_pages);
    $args = array(
      'post_type' => 'page',
      'post__in' => $not_pages;
      );
    query_posts($args);
    ?>
  5. greencode
    Member
    Posted 1 year ago #

    Getting a syntax error:

    Parse error: syntax error, unexpected ';', expecting ')' in /home/mysite/public_html/site/wp-content/themes/toolbox/template-collection.php on line 20
  6. greencode
    Member
    Posted 1 year ago #

    I used this instead

    <?php
    $not_pages = get_post_meta($post->ID, "page_id");
    print_r($not_pages);
    $args = array(
      'post_type' => 'page',
      'post__in' => $not_pages
      );
    query_posts($args);
    ?>

    But I get the following on the page:

    Array ( [0] => 363, 365 )
    Title of page

  7. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    But I get the following on the page:

    this is because your custom field, and therefore the $not_pages variable, contains a single string of comma-separated numbers - not an array;

    try to use the php function explode() to turn that into an array:

    http://php.net/manual/en/function.explode.php

  8. greencode
    Member
    Posted 1 year ago #

    Blimey! Thanks for that but I have got absolutely zero idea where to start with exploding stuff.

  9. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    I have got absolutely zero idea where to start with exploding stuff.

    did you try to read and apply the linked tutorial?

    based on you last posted code section:

    $not_pages = explode(',',get_post_meta($post->ID, "page_id"));

    (untested)

  10. greencode
    Member
    Posted 1 year ago #

    Thanks again. I just realised that I asked a similar question on here over 2 years ago but I can't for the life of me get this to work in a query posts function

    <ul>
     <?php
    $key = get_post_meta($post->ID, 'page_id', true);
    $key = explode(',', $key);
    function trim_value(&$value)
    {
        $value = (int)trim($value);
    }
    array_walk($key, 'trim_value');
    $myposts = get_posts(array('post__in' => $key, 'posts_per_page' => -1));
    foreach($myposts as $post) :
       setup_postdata($post);
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
     </ul>

    I'd like the above code to work in this format:

    <?php
    // My query here
    ?>
    <?php while (have_posts()) : the_post(); ?>
    <p><?php the_title() ?></p>
    <?php endwhile;?>
  11. greencode
    Member
    Posted 1 year ago #

    I managed to get this to work…

    <?php
    $key = get_post_meta($post->ID, 'page_id', true);
    $key = explode(',', $key);
    function trim_value($value)
    {
        $value = (int)trim($value);
    }
    array_walk($key, 'trim_value');
     $myposts = get_posts(array('post_type' => 'page', 'post__in' => $key, 'showposts' => -1, 'orderby'=> date, 'order'=> DESC));
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
    <h2><?php the_title() ?></h2>
     <?php endforeach; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.