Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help with query WordPress

  • SOLVED

Hello I was using the code below to show pages of a parent page.

I want to modify this code to show only posts/pages that have the custom field 'showonvenueparentpage'.

I am showing the below code so maybe someone could help included the styles I have below as I would like it to output the pages in the same style.

Thank you



<?php
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<div class="pageparentitem">
<?php if (has_post_thumbnail($pageChild->ID)){ ?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>
<?php } else { ?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>
<?php } ?>
<div class="pageparenttitlemusic"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></div>
</div>
<?php endforeach; endif;
?>



EDIT: Prize updated as Arnav helped me with lost more edits. : )

Answers (8)

2013-05-14

Arnav Joy answers:

try this

<?php

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>

<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<?php $field_value ='';$field_value = get_post_meta($pageChild->ID,'showonvenueparentpage',true);?>
<?php if( !empty( $field_value ) ) { ?>
<div class="pageparentitem">

<?php if (has_post_thumbnail($pageChild->ID)){ ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>

<?php } else { ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>

<?php } ?>

<div class="pageparenttitlemusic"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></div>

</div>
<?php } ?>
<?php endforeach; endif;

?>


if you want to match any specfic value for the custom field then you can try it as


<?php

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>

<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<?php $field_value ='';$field_value = get_post_meta($pageChild->ID,'showonvenueparentpage',true);?>
<?php if( $field_value == 'your_value_here' ) { ?>
<div class="pageparentitem">

<?php if (has_post_thumbnail($pageChild->ID)){ ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>

<?php } else { ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>

<?php } ?>

<div class="pageparenttitlemusic"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></div>

</div>
<?php } ?>
<?php endforeach; endif;

?>


see the line

<?php if( $field_value == 'your_value_here' ) { ?>

you can change that "your_value_here" to value you want to test.

2013-05-14

Francisco Javier Carazo Gil answers:

Something like this:

SELECT *
FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id
WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order AND pm.meta_key = 'showonvenueparentpage'


Francisco Javier Carazo Gil comments:

Better:
SELECT *
FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id
WHERE p.post_parent = ".$post->ID." AND p.post_type = 'page' ORDER BY p.menu_order AND pm.meta_key = 'showonvenueparentpage'

2013-05-14

Romel Apuya answers:

try this

<?php

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND meta_key='showonvenueparentpage' AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>

<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>

<div class="pageparentitem">

<?php if (has_post_thumbnail($pageChild->ID)){ ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>

<?php } else { ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>

<?php } ?>

<div class="pageparenttitlemusic"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></div>

</div>

<?php endforeach; endif;

?>

2013-05-14

Jens Filipsson answers:

<?php
$showonvenueparentpage=get_post_meta($post->ID,'showonvenueparentpage',true); ?>
<?php if($showonvenueparentpage) { ?>

<?php

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>

<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>

<div class="pageparentitem">

<?php if (has_post_thumbnail($pageChild->ID)){ ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>

<?php } else { ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>

<?php } ?>

<div class="pageparenttitlemusic"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></div>

</div>

<?php endforeach; endif;

?>

<?php } ?>

2013-05-14

Daniel Yoen answers:

try this :

<?php

$child_pages = new WP_Query(array('post_type' => 'page', 'post_parent' => $post->ID, 'meta_key' => 'showonvenueparentpage'));

if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>

<div class="pageparentitem">

<?php if (has_post_thumbnail($pageChild->ID)){ ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>

<?php } else { ?>

<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>

<?php } ?>

<div class="pageparenttitlemusic"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></div>

</div>

<?php endforeach; endif; ?>


hope this help :-)

2013-05-14

Yakir Sitbon answers:

The better way is use with WP_Query class.

2013-05-14

Hariprasad Vijayan answers:

Try the following code,


<?php
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<?php

$customfield=get_post_meta($pageChild->ID,'showonvenueparentpage',true); ?>
<?php if($customfield) { ?>
<div class="pageparentitem">
<?php if (has_post_thumbnail($pageChild->ID)){ ?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>
<?php } else { ?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>
<?php } ?>
<div class="pageparenttitlemusic"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></div>
</div>
<?php } ?>
<?php endforeach; endif;

?>

2013-05-14

Eric P. answers:

I agree with @Yakir Sitbon.

Use a WP_Query object.

Something like what @Daniel Yoen suggested.