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

Query to look for pages with certain tag WordPress

  • SOLVED

Hello, I am using the following query in a page template to show all pages that are a child of the parent page, however my problem arises when I need a page to have 2 parents, can I use tags for this purpose? i.e. group pages by tag.

Can someone help me modify the following code to show all pages with the tag 'music'?


<?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;
?>

Answers (5)

2013-04-16

Dbranes answers:

You can add the tags support to your pages with

add_action( 'admin_init', function(){
register_taxonomy_for_object_type( 'post_tag', 'page' );
});


and then you can use <strong>WP_Query($args)</strong> to construct your query.

If you want to add category support you can replace '<em>post_tag</em>' with '<em>category</em>'.


Dbranes comments:

You can try this to fetch all your pages that are tagged with "<em>music</em>":

<?php
$args = array(
'post_type' => 'page',
'tag' => 'music',
'posts_per_page' => -1,
);
$mypages = new WP_Query( $args );?>
<?php if ( $mypages->have_posts() ):?>
<ul>
<?php while ( $mypages->have_posts() ) : $mypages->the_post(); ?>
<li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata();?>


<strong>ps:</strong> Thanks for your comments @Yakir and @Daniel, I should have mentioned that <em>post_tag</em> and <em>category </em> are just the default taxonomies. In many cases <em>post_tag</em> are unused, but in some cases it's better to use a custom taxonomy for this.


Dbranes comments:

ps: it looks like @Subharanjan Mantri and @Hai Bui are missing the <strong>the_post()</strong> part in the while-loop, so it becomes infinite.

2013-04-16

Hai Bui answers:

Please try this

<?php
$args = array(
'post_type' => 'page',
'tag' => 'music'
);
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : ?>

<div class="pageparentitem">

<?php if (has_post_thumbnail(get_the_ID())){ ?>

<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<?php } else { ?>

<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>

<?php } ?>

<div class="pageparenttitlemusic"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>

</div>
<?php

endwhile; endif;

wp_reset_postdata();
?>


Ross Gosling comments:

Thank you for the reply, however when I use this code it outputs the current page infinitely, it crashes my browser as if the loop never closes.

2013-04-16

Daniel Yoen answers:

Sorry, I think, page don't have tag, you use custom taxonomy ?


Daniel Yoen comments:

add the tags support(Dbranes says)

add_action( 'admin_init', function(){
register_taxonomy_for_object_type( 'post_tag', 'page' );
});


And your code maybe like this ;

<?php

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)

WHERE post_parent = ".$post->ID." AND $wpdb->term_taxonomy.taxonomy = 'post_tag' AND $wpdb->terms.slug = 'music' 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; ?>


hope this help :-)

2013-04-16

Yakir Sitbon answers:

@Dranes, If you use with "tag" i think the better way, use with Custom Taxonomy.

2013-04-16

Expert answers:

<strong>1. Paste this inside the function.php of current theme, then open admin end and tag the pages.</strong>

/**
* Enable tagging for pages
*/
function display_tag_interface() {
// Add tag metabox to page
register_taxonomy_for_object_type('post_tag', 'page');
}
add_action( 'admin_init', 'display_tag_interface' );



<strong>2. Modify the page template file.</strong>

<?php
$args = array(
'post_type' => 'page',
'tag' => 'music'
);
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post()?>
<div class="pageparentitem">
<?php if (has_post_thumbnail(get_the_ID())){ ?>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>
<?php } ?>
<div class="pageparenttitlemusic"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>
</div>
<?php
endwhile; endif;
wp_reset_postdata();
?>


Ross Gosling comments:

Thank you for the reply, however when I use this code it outputs the current page infinitely, it crashes my browser as if the loop never closes.