get posts of taxonomy parent? - php

Im new with custom post type - taxonomy wordpress.
I created a template page to show list of products in custome taxonomy but i dont know how exactly to do.
Example:
Category
SubCategory
Product A in Category and B in Category>SubCategory.I want to click Category will show only Product A and if i click SubCat will show only Product B.
Tks for helping!
<?php
$wp_query->get_queried_object_id();
$args = array(
'post_type' => array('san-pham'),
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 100,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
);
$eq_query = new WP_Query( $args );
$wp_query->get_queried_object_id();
$offset = 0;
$count = 0;
$eq_total_posts = $eq_query->found_posts - $offset;
if ($eq_query->have_posts()) : // The Loop
$eq_count = $args['paged'] * $args['posts_per_page'] - $args['posts_per_page']; // Count items
?>
<?php
while ($eq_query->have_posts()): $eq_query->the_post();
$eq_count++;
$count++;
?>
<?php
if ($count == 4) {
$p4 = 'four';
$count = 0;
} else { $p4 = ''; }
?>
<div <?php post_class($p4); ?> id="pro-cat">
....
</div>
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>

Related

Output text only when search has been run

I have a custom post type that I'm searching through with a custom query. I want to output a message if there's too many results (that works fine). But the problem I have is when you first goto the page and no search has been performed, I don't want that message to appear. How can I stop it appearing when no search has been performed?
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 5,
'post_type' => 'researchdatabase',
'post_status' => 'publish',
'paged' => $paged
);
if($searchTerm != "") {
$args['s'] = $searchTerm;
}
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
$counter = 1; ?>
<p>
<b><?php echo $the_query->post_count; ?> research items</b>
<?php if($the_query->post_count > 20) { ?>
<br /><span class="research-alert"><b>Refine your search criteria to see fewer results.</b></span>
<?php } ?>
</p>
<hr />
<br />
<?php while ($the_query->have_posts()) { $the_query->the_post(); ?>
<?php // output results ?>
<?php $counter++;
} // end while
// reset post data
wp_reset_postdata();
} else {
echo 'No results';
} // end if
?>
Just change the condition:
<?php if($the_query->post_count > 20) { ?>
With:
<?php if($the_query->post_count > 20 && is_search()) { ?>
is_search() return true if any search term is sent for the current WP_Query.
"no search has been performed" equals "no search term in the query", why don't you just check search term first?
if(!empty($searchTerm)) {
$args = array(
's' => $searchTerm
'posts_per_page' => 5,
'post_type' => 'researchdatabase',
'post_status' => 'publish',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
);
$the_query = new WP_Query($args);
// Your code.
}
BTW: if you want to display total count of search results, you should use $the_query->found_posts not $the_query->post_count it only count posts on first page.

Display post list by custom post type and category

Hi i need to display list of post by post type and category, i have code like this, but it isint working properly:
<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename&cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?>
This code displaying post from "posttypename", but it displays all post from that custom post type, but i need to displaying post from only "categoryname"
The whole code looks like this:
<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename%cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?> <?php if($i == 1) : ?> <div class="">content of the post</div> <?php endif; ?> <?php $i++; endwhile; ?>
Use WP_Query like this :
$catquery = new WP_Query(array(
'post_type' => 'posttypename',
'posts_per_page' => -1,
'category_name' => 'categoryname',
// 'cat' => cat ID here
));
if( $catquery->have_posts() ){
while($catquery->have_posts()){
$catquery->the_post();
// your stuff
}
wp_reset_postdata();
}
Try below code
<?php
$catquery = new WP_Query(array(
'post_type' => 'posttypename',
'posts_per_page' => -1,
'category_name' => 'categoryname'
));
if( $catquery->have_posts() ){
$i = 1;
while($catquery->have_posts()){
if($i == 1){ ?>
<div class="">content of the post</div>
<?php } ?>
$i++;
}
wp_reset_postdata();
}
?>

Wordpress Related Posts by TAG (Custom Query)

:)
So, I am trying to create a "related posts" thing using the TAGS of the post is being read at the moment. My theme -- which was custom designed -- have a related posts, but based on the CATEGORY.
I tried to changed it to tag, but absolutely no success (just a blank screen. ;D)
Can you help me with that? :)
query_posts("post_type=any&cat=13&posts_per_page=8");
global $wp_query;
$totalposts = $wp_query->found_posts;
$i = 1;
if (have_posts()):
while (have_posts()):
the_post();
include (TEMPLATEPATH . '/inc/categorias.php');
$thumb_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'full'));
$class_name = (empty($thumb_image))?("empty_image"):("");
$bigpost = ($i % 9 == 0)?"big-post $catURL":"$catURL";
$terms = get_terms( 'notepad', array(
'orderby' => 'count',
'hide_empty' => 0
));
Ok, I did it. :)
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>8,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
$totalposts = $wp_query->found_posts;
$i = 1;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
include (TEMPLATEPATH . '/inc/categorias.php');
$thumb_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'full'));
$class_name = (empty($thumb_image))?("empty_image"):("");
$bigpost = ($i % 9 == 0)?"big-post $catURL":"$catURL";
And then, after the loop...
$i++;
endwhile;
}
wp_reset_query();
}

Insert a post type every x posts in custom loop

I was hoping someone could help me with this problem.
I'm trying to make a loop in wordpress with two different post types ('product' and 'outfits')
The code below is working fine, this outputs a list of the two post types ordered by newest to older.
$loop = new WP_Query( array(
'post_type' => array( 'product', 'outfits' ),
'posts_per_page' => 15
) );
$counter = 0;
?>
<?php if ( $loop->have_posts() ) { while ( $loop->have_posts() ) { $loop->the_post();
$counter++; ?>
<?php $loop->is_home = false; ?>
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
$titulo = get_the_title();
$content = apply_filters ('the_content', $post->post_content); ?>
<div class="caja-<?php echo $counter; ?>">
<?php if ( $post->post_type == "product" ) {
//some stuff
<?php } else {
//some stuff
} ?>
</div>
<?php } } wp_reset_postdata();
What I would like to do is to insert a product post type after X number of outfits.
I was trying to merge a similar solution I've found in other question with no luck. I'm not a php expert so any help is appreciated
<?php
$args = array('post_type'=>'post', 'posts_per_page'=>9, 'category_name'=>'news');
$posts = get_posts($args);
$args = array('post_type'=>'testimonials', 'posts_per_page'=>3);
$testimonials = get_posts($args);
// see how many of the regular posts you got back //
$post_count = count($posts);
// see how many testimonials you got back //
$testimonial_count = count($testimonials);
// add them up to get the total result count //
$total_count = $post_count + $testimonial_count;
// Loop through the total number of results //
for($i = 1; $i <= $total_count; $i++){
// assuming you want to show one testimonial every third post //
if($i % 3 == 0){
// this means you're on the a third post, show a testimonial //
setup_postdata($testimonials[$i]);
}
else{
/** show a regular post */
setup_postdata($posts[$i]);
}
/** and now handle the output */
?><h1><?php the_title();?></h1><?php
} ?>
$x = 3;
$products = get_posts(array(
'post_type' => 'product',
'posts_per_page' => -1
));
$outfits = get_posts(array(
'post_type' => 'outfit',
'posts_per_page' => -1
));
foreach ($outfits as $num => $outfit) {
if ( ($num+1) % $x == 0) {
if (isset($products[($num+1)/$x - 1])) {
array_splice( $outfits, $num, 0, array($products[($num+1)/$x - 1]) );
}
}
}
foreach ($outfits as $outfit) {
$posts_id[] = $outfit->ID;
}
$query = new wp_query(array(
'post_type' => array('outfit', 'product'),
'post__in' => $posts_id,
'posts_per_page' => 15,
'orderby' => 'post__in'
));

Pagination in wp_query

This may be an easy but I cannot get it figured out. I have searched for hours with no luck on my situation. I need to get pagination to work with my_query
<?php
$count = 0;
$id_suffix = 1;
$items_per_row = 4;
$quality = 90;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query( array( 'posts_per_page' => '4', 'post_type' => 'portfolio') );
$grid_class = 'grid_3';
$desired_width = 220;
$desired_height = 190;
$terms = get_terms( 'portfolio_categories' );
$count_terms = count( $terms );
?>
//some php code
<?php while ( $wp_query -> have_posts()) : $wp_query -> the_post(); //query the "portfolio" custom post type for portfolio items ?>
//some more php code
<?php endwhile;?>
<div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">«</span> Older posts', 'thematic')) ?></div>
<div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">»</span>', 'thematic')) ?></div>
</ul>
I got the pagination to display but when clicking on another page it shows the same portfolio items. Any help would be appreciated.
I got the pagination to show using wp-pagenavi but the same issue with not changing the items.
You are constructing the $paged variable but aren't using it.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'portfolio',
'paged' => $paged // this is the missing part
) );
WordPress uses the $wp_query variable name. You should probably use a different one.
Thank you #maiorano84 and #s_ha_dum for pointing me in the right direction. I wanted to update this post for anybody that runs into the problem. The problem I was having was making the pagination work on the a static front page. After reading in the codex I figured out my problem.
Instead of $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
I had to use
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
That one little mistake is what kept the pagination to work. so the final code looked like this.
<?php
$count = 0;
$id_suffix = 1;
$items_per_row = 4;
$quality = 90;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$my_query = new WP_Query( array(
'posts_per_page' => 8,
'post_type' => 'portfolio',
'paged' => $paged
) );
$grid_class = 'grid_3';
$desired_width = 220;
$desired_height = 190;
$terms = get_terms( 'portfolio_categories' );
$count_terms = count( $terms );
?>
//Some php code
<?php while ( $my_query -> have_posts()) : $my_query -> the_post(); //query the "portfolio" custom post type for portfolio items ?>
(Some more php code)
<?php endwhile;
wp_pagenavi(array( 'query' => $my_query ) ); ?>
</ul><!-- END .portfolio-gallery -->
?>

Categories