• Hey Guys!
    Long time reader, first time poster…

    Im in a bit of a jam. I need a way to display the custom fields of pages child but only display the children whose custom field is a certain value.

    I’m making an online store and the children of the page are the products (that will display on the store page). How can I just show i.e. the products that have a certain custom field value.

    The code below shows every child, but i need to add a condition to it… its killing my brain! haha.

    $children = get_pages('child_of=' . $post->ID. '&depth=-1' );
    
    		if (!empty($children)) { 
    
        	foreach($children as $child) {
    
            // Get the 2 meta values from the child page
            $shortdesc = get_post_meta($child->ID, 'short-description', true);
            $photo = get_post_meta($child->ID, 'product-photo', true);
            $price = get_post_meta($child->ID, 'price', true);
            $title = get_the_title($child->ID);
    
            // Display the meta values
            echo '
            <ul>
            <div class="ProductContainer">
            <h6> ' . $title . '</h6>
            <div id="shortdesc" class="floatleft">' . $photo . '</div>
            <p>' . $shortdesc . '</p>
            </div>
            <div id="submitbutton" class="floatright">
            <form action="/cart" method="post" accept-charset="utf-8">
    			<input type="hidden" name="name" value=" ' . $title . '" />
    			<input type="hidden" name="price" value=" ' . $price . '" />
    			<input type="submit" name="Add to Cart" value="Add to Cart" class="submit" />
    			</form>
          	</div><div id="ProductPrice">$' . $price . ' ' . $condition . ' + P&H</div>
            </ul>';
            }
    }    
    
    				?>

    If anyone can assist I will shout from the heavens and flutes and confetti etc etc. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hmm… Can you try to replace this

    $children = get_pages('child_of=' . $post->ID. '&depth=-1' );

    with this

    $children = get_pages(
      array (
        'child_of' => $post->ID,
        'depth' => -1,
        'meta_query' => array(
          'key' => 'custom-key',
          'value' => 'true',
          'compare' => 'custom-value'
          )
      )
    );

    That should only get the pages, wich have set the “custom-key” meta field to “custom-value”

    let me know if it helps you ??

    Thread Starter troyco85

    (@troyco85)

    Thanks egado for an amazingly quick response!

    I quickly threw it in and it’s definitely getting the children pages but for some reason its still bringing every child in… I love the array approach though!

    Damn, naughty children ??

    Hmmm… I have no test here, but maybe you can try a query instead of get_pages… like this… (guess you have to do some extra changes, and you wont need the foreach)…

    <?php
    
    query_posts(
            array(
            'post_type'  => page,
            'child_of' => $post->ID,
            'depth' => -1,
            'meta_query' => array(
              'key' => 'custom-key',
              'value' => 'true',
              'compare' => 'custom-value'
              )
          )
    ); 
    
    while (have_posts()) : the_post(); ?>
    
    <!-- some code -->         
    
    <?php endwhile; wp_reset_query(); ?>
    Thread Starter troyco85

    (@troyco85)

    yeah, looks like you’re onto something there… I’ll put a portion of my genius towards it and I’ll definitely let you know how I go!

    Thanks soo much!

    I’ll keep my fingers crossed. ??
    Will read it later, need some sleep now…

    Good luck ??

    Thread Starter troyco85

    (@troyco85)

    Still attempting it. Man, I feel like I’m so close and it will be something obvious.

    I tested out your meta_query and it seems that it doesn’t get used at all (i.e. I can change the key, value, compare to anything and it won’t change the outcome).

    I haven’t lost hope yet though haha!

    Thread Starter troyco85

    (@troyco85)

    Ok, so i found a non elegant solution…

    $children = get_pages(
      			array (
        		'child_of' => $post->ID,
        		'depth' => -1,
         			)
    		);
    
        	foreach($children as $child) {
    
            // Get the meta values from the child page
            $shortdesc = get_post_meta($child->ID, 'short-description', true);
            $photo = get_post_meta($child->ID, 'product-photo', true);
            $price = get_post_meta($child->ID, 'price', true);
            $category = get_post_meta($child->ID, 'category', true);
            $title = get_the_title($child->ID);
    
    		if($category == value) {
            // Display the meta values
            echo '
    
                         <Output Code goes here>
            }
       }
    
    				?>

    Basically retrieved all the pages and use an if statement to hide all the pages I don’t want.

    I’l have to come back to this issue in the future but for now it’s got me out of a massive jam. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display pages child with certain custom field’ is closed to new replies.