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
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
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);
?>
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
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
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
Blimey! Thanks for that but I have got absolutely zero idea where to start with exploding stuff.
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)
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;?>
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; ?>