Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need help!

I'm doing this template page and I want to get content from pages with the same meta_key. I want to display all pages thumbnails that has the value "red" in the meta_key.

The problem is that the meta_key value in the db is a string... I get nothing with this:

$Red = 'a:1:{i:0;s:9:"Red";}';
$Red = unserialize($Red);

$pages = get_pages(array('meta_key' => 'color', 'meta_value' => $red[0]));
 foreach ($pages as $page):

    <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
    <?php echo $page->post_title; ?>

    <?php echo $page->post_content; ?>
    <?php endforeach; ?>

What am i doing wrong?

share|improve this question

1 Answer 1

Your code snippet is full of errors.

  • The serialized string is invalid, it's only 3 chars instead of 9, unserialization will not work, it will trigger an error.
  • Variables are case sensitive, $red doesn't exist, it's $Red, this will trigger an error too.
  • The <?php ?> tags are redundant and invalid since there already must be an opening PHP tag, ofcourse this will also trigger an error.

Also I don't understand why the meta value being a string should be a problem?

Works fine:

$red = 'a:1:{i:0;s:3:"Red";}';
$red = unserialize($red);

$pages = get_pages(array('meta_key' => 'color', 'meta_value' => $red[0]));
foreach($pages as $page)
{
    echo get_the_post_thumbnail($page->ID, 'thumbnail');
    echo $page->post_title;
    echo $page->post_content;
}
share|improve this answer
    
I get a:1:{i:0;s:9:"Red";} from the mySQL db, i'dont made that up :) . My problem is that I dont get any results when i write this code. I get nothing putting out 'meta_value' => $red[0])); from the code either. –  Andy79 Oct 15 '12 at 17:39
    
I see, however that needs to be fixed if you must work with these serialized values. Instead of a variable, try using a hard coded value as a test, ie 'meta_value' => 'Red', also make sure that you are using the correct upper and lower case, ie that color is actually lower case, and Red is upper case in the database. –  ndm Oct 15 '12 at 18:02
    
thank you so much for your help. I fixed the string now, sonow it says only "red" in the database, i trying this but it wont work! <?php get_pages("meta_key=color&number=3&title_li=&"); ?> but this work <?php wp_list_pages("meta_key=color&number=3&title_li=&"); ?> do you know why get_pages dosent work for me –  Andy79 Oct 15 '12 at 19:29
    
There are lots of possible reasons... the most obvious is that wp_list_pages passes some modified defaults to get_pages, specifically exclude and hierarchical, where hierarchical is set to 0 as opposed to 1 when using get_pages directly. exclude might be different in case filters are active for wp_list_pages_excludes. To find out what exactly is different, do a var_dump on $r in the get_pages and wp_list_pages functions. –  ndm Oct 15 '12 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.