Only looping through pages that have children or subpages

I just figured out the answer to my own question. It works by counting the number of children pages using get_pages. Here is the working code: <?php $children = get_pages( array(‘child_of’ => $post->ID,’post_type’=>’custom-post-type-name’)); if( count( $children ) != 0 ) { ?> <h1><a href=”https://wordpress.stackexchange.com/questions/46090/<?php the_permalink(); ?>”><?php the_title(); ?></a></h1> <?php } else { } ?>

How to check all items in a loop have post thumbnail?

Before you do your output, do something like this: $no_thumbnails = true; foreach( $shows_sales as $p ) { if( has_post_thumbnail( $p->ID ) ) $no_thumbnails = false; } Then in your loop you can do something like this: <?php if( $no_thumbnails === true ) : ?> <!– Your output here –> <?php endif; ?>

How to modify the Loop from a plugin (instead of a theme)

for a simple meta query you can do this: function wpa_47150( &$query ) { if(!is_admin()){ set_query_var(‘meta_key’, ‘test_field’); set_query_var(‘meta_value’,’bacon’); } } add_action(‘pre_get_posts’,’wpa_47150′); i wasn’t sure it was going to work, but apparently you can set the whole meta_query object this way too function wpa_47150( &$query ) { if(!is_admin() && is_post_type_archive( ‘test_post_type’ )){ $metaq = ( array … Read more

Help with if and else statement

What you’re gonna want to do is completely rewrite that super-complex structure you have to something like this: <?php if( $my_query->have_posts() ) : ?> <?php while( $my_query->have_posts() ) : $my_query->the_post(); ?> <!– your output here –> <?php endwhile; else : ?> <!– your failure output here –> <?php endif; ?>

Custom query does not find pages

This is not the place to use query_posts. query_posts should only be used if you need to change the main loop. There is also no need to reset the query and your missing endif on both loops. I would use get_pages instead of running a new WP_Query. $pages = get_pages(); foreach ( $pages as $page … Read more

Ascending order has strange effects on wordpress loop

It seems the problem is solved by adding ‘posts_per_page’ => -1 which forces WordPress to show all posts matching the parameters. Here’s my final code: array ( ‘post_type’ => ‘symposia_program’, ‘days’ => $progDay, ‘meta_key’ => ‘class_number’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘ASC’, ‘posts_per_page’ => -1 );