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

Custom Post Query by Term WordPress

  • SOLVED

I am trying to create a list of posts in a queried taxonomy term - AND display them on a page template (i.e. page-template.php)

Example, I have a X terms listed on a page. I'm trying to create a popover box that displays on hover of these terms that shows a list of the posts inside that [already queried] term.

I tried creating a custom loop using the code below. However, it doesn't show posts currently because I am not sure how to set it to show posts dynamically based on the taxonomy term queried. Right now I just stuck $term in the place where the slug should go. Obviously I can't put a slug here because I need it to be generated dynamically. Is this harder than I think? Or can this bit of code be modified slightly to work?

<?php $wp_query = new WP_Query( array ( 'post_type' => 'grid100index', '100gridgroups' => '$term', 'posts_per_page' => '8', 'paged' => get_query_var( 'paged' ) ) ); ?>


P.S. I have done this separately via a taxonomy-100gridgroups.php template and it works fine - presumably because the term query is already in place. The fuzzy part for me here is calling these posts by term into an isolated page template.

FULL CODE FOR POST QUERY (symbol-list-popover.php)

<?php $wp_query = new WP_Query( array ( 'post_type' => 'grid100index', '100gridgroups' => '$term', 'posts_per_page' => '8', 'paged' => get_query_var( 'paged' ) ) ); ?>

<!-- Symbol Table -->
<div class="table-responsive">
<table width="100%" class="table table-bordered">
<thead>
<tr>
<th width="8%">&nbsp;</th>
<th width="21%" align="left">Equity <small>(Symbol)</small></th>
<th width="21%" colspan="3" ><div class="text-center">Bid Lines</div></th>
<th width="21%" colspan="3" ><div class="text-center">Offering Lines</div></th>
<th width="8%" align="center"><div class="text-center">OPS <a href="#" data-toggle="tooltip" data-placement="top" title="Origin of Positive Sentiment"><small><span class="glyphicon glyphicon-info-sign icon-white"></span></small></a></div></th>
<th width="21%" colspan="3"><div class="text-center">Critical Mass Line</div></th>
</tr></thead>
<tbody>
<tr class="info">
<td align="left"><small>&nbsp;</small></td>
<td align="left">&nbsp;</td>
<td align="center"><small>1</small></td>
<td align="center"><small>2</small></td>
<td align="center"><small>3</small></td>
<td align="center"><small>1</small></td>
<td align="center"><small>2</small></td>
<td align="center"><small>3</small></td>
<td align="center"><small>&nbsp;</small></td>
<td align="center"><small>1</small></td>
<td align="center"><small>2</small></td>
<td align="center"><small>3</small></td>
</tr>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<tr>

<td align="left"> <?php if ( has_post_thumbnail() ) {
the_post_thumbnail('list-thumb', array('class' => 'img-rounded'));
} else { ?>
<img class="img-rounded img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/08/RA-logo-150px-groups.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
<div class="text-center"><?php echo add_update_status(); ?></div></td>
<td>

<small><a class="text-muted" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="pull-left">Open All Symbol Data</a></small>
</td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('bid_line_1')?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('bid_line_2')?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('bid_line_3')?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('offering_line_1')?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('offering_line_2')?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('offering_line_3')?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('origin_of_positive_sentiment') ?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('critical_mass_line_1') ?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('critical_mass_line_2') ?></small></td>
<td align="center"><small><span class="text-muted">$</span><?php the_field('critical_mass_line_3') ?></small></td>

<?php endwhile; else: ?>
<p><?php _e('<div class="alert alert-danger"> Sorry, no symbols currently available for this group.</div>'); ?></p>

<?php endif;?>
</tr>
</tbody>
</table>
</div>
<!-- Symbol Table End-->


TEMPLATE I'M CALLING THE ABOVE INTO

<?php
/*
Template Name: 100 List Page
*/
?>
<?php get_header(); ?>
<div class="row heading-grid">
<div class="container">
<div id="heading-position">
<div class="col-lg-12">
<h2><span class="heading-grid-title"><span class="glyphicon glyphicon-th icon-white"></span> <?php the_title(); ?></span></h2>
<div id="breadcrumbs"> <small><?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?></small></div>
</div>
</div>
</div><!-- close container -->
</div><!-- close row services -->
<div id="spacer"></div>


<div class="container">
<div class="row">
<div class="col-lg-9 col-md-9 col-sm-10 col-xs-12">


<article> <!-- Grid Key -->
<div class="row">
<div class="col-lg-12">
<?php include("grid-key.php"); ?>
</div></div>
<!-- Grid Key -->
<!-- Content Protection -->
<?php ob_start();?>
<!-- Content Protection -->



<div class="row" >
<?php
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var('paged');
}elseif( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
}else{
$paged = 1;
}
$per_page = 12;
#fix
$term_args = array(
'hide_empty' => 0,
'exclude' => array(16, 20, 22,25, 27, 28, 30, 4, 33,), //* Enter ID's of parent categories to exclude from list

);
$number_of_terms = wp_count_terms( '100gridgroups' , $term_args); // This counts the total number terms in the taxonomy with a function)
$paged_offset = ($paged - 1) * $per_page;
$libargs = array(
'orderby' => 'name',
'order' => 'ASC',
'exclude' => array(16, 20, 22,25, 27, 28, 30, 4, 33, ), //* Enter ID's of parent categories to exclude from list
'number' => $per_page,
'offset' => $paged_offset,
);
$_libargs = wp_parse_args($term_args, $libargs);
$libcats = get_terms( '100gridgroups', $_libargs);

#fix
$i = 0;
foreach($libcats as $lc){
if( $i % 4 == 0 ) { ?>
<div class="clearfix"></div>
<?php }
$i++; ?>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" >
<?php $termlink = get_term_link( $lc->slug, '100gridgroups' ); ?>
<div class="panel panel-default">
<div class="panel-image">

<div class="thumbnail">
<div class="caption">
<br/>
<h1><span class="label label-primary"><?php echo $lc->count ?></span></h1>
<p> Symbols </p>
<h4> <a class="label" href="<?php echo $termlink; ?>">Open Group</a> </h4>

<!-- Popover Start -->


<div class="po-markup" > <a class="p5598 popover-dismiss" >Preview Symbols </a>


<div class="po-content hidden popover">
<div class="po-title">
<h4><?php echo $lc->name; ?> Symbols <span class="pull-right"><?php echo $lc->count ?> Active</span></h4>


</div> <!-- ./po-title -->



<div class="po-body">

<strong><?php include("symbol-list-popover.php"); ?></strong>
This is the body

<a class="btn btn-default btn-sm" href="<?php echo $termlink; ?>">View All Symbols</a>

</div><!-- ./po-body -->

</div> <!-- ./po-content -->
</div><!-- ./po-markup -->

<!-- Popover End -->


</div>

<!-- Get Image by Attachment ID Start-->
<?php
$attachment_id = get_field('taximage', '100gridgroups_'.$lc->term_id);
if ($attachment_id) {
$image = wp_get_attachment_image_src($attachment_id, 'industrygroup-img');
if ($image) {
?>
<img class="img-responsive" src="<?php echo $image[0]; ?>" />
<?php
}
}
else { ?>
<img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/09/RA-logo-300px-groups.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
<!-- description div -->
<div id="deschide">
<div class="<?php if(get_field('level_of_interest', '100gridgroups_'.$lc->term_id)) {
echo the_field('level_of_interest', '100gridgroups_'.$lc->term_id);

} else { ?>intleveldefault<?php } ?>">
<!-- description content -->
<div class="description_content"> <a href="<?php echo $termlink; ?>"><?php echo $lc->name; ?></a> </div>
<!-- end description content -->
</div> </div>

<!-- end description div -->

</div>

<!-- Get Image by Attachment ID End-->



</div>

<div class="panel-footer text-center">
<!-- Icons -->
<div class="row"> <div class="col-lg-4">
<div class="<?php if(get_field('heat_level', '100gridgroups_'.$lc->term_id)) {
echo the_field('heat_level', '100gridgroups_'.$lc->term_id);

} else { ?>heatleveldefault<?php } ?>"></div>
</div>
<div class="col-lg-8">
<div class="<?php if(get_field('star_rating', '100gridgroups_'.$lc->term_id)) {
echo the_field('star_rating', '100gridgroups_'.$lc->term_id);

} else { ?>stardefault<?php } ?>"></div></div>
</div>
<!-- Icons -->
</div>
</div>
</div>



<?php } ?>
</div>
<!-- Pagination Start -->
<div class="text-center">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => ceil( $number_of_terms / $per_page ), // 20 items per page
'type' => 'list',
'end_size' => 5,
'mid_size' => $mid_size,
)
);
?></div>
<!-- Pagination End -->

<!-- Content Protection -->
<?php echo do_shortcode('[private]'.ob_get_clean().'[/private]' ); ?>
<!-- Content Protection -->
</article>

</div> <!-- End col-md-9 -->
<div class="col-lg-3 col-md-3 col-sm-2 col-xs-12">

<span class="hidden-sm hidden-xs"> <?php include("sidebar100list.php"); ?></span>
</div> <!-- End col-md-3 -->
</div><!-- End row -->
</div><!-- End Container -->
<?php get_footer(); ?>

Answers (2)

2014-10-16

Remy answers:

Can we get a full picture of the code inside your template ?


streetfire comments:

Please see update. I'll try to answer any questions.


Remy comments:

The code for your query should be

<?php $wp_query = new WP_Query( array (
'post_type' => 'grid100index',
'tax_query' => array(
array(
'taxonomy' =>'100gridgroups',
'field' => 'slug',
'terms' => $lc->slug
)
),
'posts_per_page' => '8',
'paged' => get_query_var( 'paged' )
) ); ?>


Remy comments:

Also, it's better to use get_template_part() when doing include in template files, instead of the include() function


streetfire comments:

Remy, you are a genius! Thank you so much for the quick help, I greatly appreciate it!!!

2014-10-16

Dbranes answers:

Did you try for example:


if( is_tax() ) {
$term_obj = get_queried_object();
$term = $term_obj->slug;
}


or

$term = get_query_var( '100gridgroups' );

where you would use it in your query part where:

'100gridgroups' => $term


streetfire comments:

No I haven't done it quite like that. How would that be integrated... something like this?

<?php $wp_query = new WP_Query( array ( 'post_type' => 'grid100index', '100gridgroups' => '$term', 'posts_per_page' => '8', 'paged' => get_query_var( 'paged' ) ) ); ?>

<?php if( is_tax() ) {

$term = get_queried_object();

$slug = $term->name;

};?>


Dbranes comments:

ok, didn't see the question update with the actual code.

I think @Remy solved it for you ;-)


streetfire comments:

No worries, thanks though!