i need to display post from a custom post type but from one specific category, i use the code belowe but show me all post from all categories not just from 7.
<?php
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'cat=7' , 'taxonomy' => 'tourcat');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo "TEST TEST TEST TEST";
echo the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
This is not a valid argument for WP Query.
cat=7
Please read here about taxonomy queries, they should do what you require:
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Currently your $args array simply holds an element that is cat=7, which is an incorrect way to pass arguments to the WP_Query constructor. Note that the issue is a little more obvious if you format your $args array with whitespace:
$args = array(
'post_type' => 'tour',
'posts_per_page' => 10,
'cat=7' , /* Here is the problem */
'taxonomy' => 'tourcat'
);
I believe your $args array should look like the following:
$args = array(
'post_type' => 'tour',
'posts_per_page' => 10,
'cat' => 7,
'taxonomy' => 'tourcat'
);
This,
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'cat=7' , 'taxonomy' => 'tourcat');
Should instead be like,
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'tax_query' => array( array( 'taxonomy' => 'tourcat','field' => 'ID','terms' => '7' ) ));
Try this :
$postData = new WP_Query(array(
'post_type' => 'tour', // custom post type
'posts_per_page'=>10,
'tax_query' => array(
array(
'taxonomy' => 'tourcat', //custom taxonomy name
'field' => 'id',
'terms' => 7
)
)
));
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo "TEST TEST TEST TEST";
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;
Related
I want to hide the text with the word reviews if query has no posts. Please ask for suggestions.
is image > enter image description here
<h1 class="section-title"><span>Reviews</span></h1>
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat' => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post();
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>
No need of jQuery, just put the h1 tag after the If statement and before while. Then if your query is not empty it will display the title otherwise not
Like this:
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat' => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) { ?>
<h1 class="section-title"><span>Reviews</span></h1>
<?php while ($second_query->have_posts() ) : $second_query->the_post();
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>
I want to display all post in my WordPress home page.
I have written following query for getting all post but I do not get all posts. It just displays 10 or 11 posts:
$args = array(
'post_type' => 'post',
'posts_per_page' => $number,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
$args['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-video',
));
$query = new WP_Query($args);
So please let me know how can I get all posts.
Displaying all posts that has been published. You have to use post_per_page='-1' to retrive all the posts.
$args = array(
'post_type'=> 'post',
'orderby' => 'ID',
'post_status' => 'publish',
'order' => 'DESC',
'posts_per_page' => -1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : ?>
<?php while ( $result->have_posts() ) : $result->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
Hope so this will retrive all the posts as per your expectation.
try Multiple loops :
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- do stuff ... -->
<?php endwhile; ?>
<?php endif; ?>
if you need Advanceded query use this :
<?php query_posts( 'post_type=post&posts_per_page=10'); ?>
and you can use query_posts() get all posts
Set the variable posts_per_page => -1
As so:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
This will make Query get all posts in your table.
See more in the WP Query docs
I'm building a template for a homepage which shows 4 latest posts on top and some groups of posts divided by category around the page (I'm using get_posts to perform queries).
What I'd like to do is exclude from these category posts any post already present in the latest four news on top.
I guess I should get that four post IDs and use the 'post__not_in' parameter in the "category" queries, but I can't make it work.
Dou you have any hints?
This is the code:
// First query: I get last four posts
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
// In my dreams, this should be the array of post IDs
$ids->query_vars['page_id'];
// Second query: I get 5 posts for a given categoory and exclude posts with IDs from first query
$query = new WP_Query($args);
$args2 = array(
'numberposts' => 5,
'category' => 15,
'orderby' => 'post_date',
'order' => 'DESC',
'post__not_in' => $ids
);
$query2 = new WP_Query($args2);
$primaposizione = get_posts( $args2 );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
... do the stuff ...
endforeach;
wp_reset_postdata();
UPDATE
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post();
$excludeID = $post->ID;
endwhile;
$args = array(
'posts_per_page' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'category' => 15,
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($excludeID)
);
$primaposizione = get_posts( $args );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
$category = get_the_category();
$slug = $category[0]->category_nicename ;
$esteso = $category[0]->cat_name;
if(has_post_thumbnail()) { ?>
<span class="hidden-xs"><?php the_post_thumbnail('bones-thumb-300', array('class' => 'img-responsive')) ?></span>
<?php } ?>
<h3 class="ellipsis"><?php the_title(); ?></h3>
<?php the_excerpt();?>
<?php endforeach;
wp_reset_postdata();
?>
The way you're trying to select the ID's is not going to work. You have to call wp_query first. I think this should work:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post(); ?>
$excludeID[] = $post->ID; // forgot the brackets
// do stuff
endwhile;
(...)
in my single.php i want to show all category posts except the opened post.
Here is my code: pastebin.com loop code
Here is the code for category posts loop, it loops all category posts and the opened one, i do not want to loop opened post again
<?php $catid = the_category_ID( false ); ?>
<?php $postCount = 1; $loop = new WP_Query( array( 'tax_query' => array(array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $catid
)), 'post_type' => 'post', 'posts_per_page' => 15 ) ); if ($loop->have_posts()) { ?>
UPDATE:
I have the solution:
<?php $catid = get_the_category(); $catid = $catid[0]->term_id; ?>
<?php $postCount = 1; $loop = new WP_Query( array( 'tax_query' => array(array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $catid
)),
'post_type' => 'post',
'posts_per_page' => 15,
'post__not_in' =>array($post->ID) ) );
if ($loop->have_posts()) { ?>
U need to use 'post_not_in' option.
Use this : (example avoiding category 3)
query_posts( 'cat=-3' );
Check this : http://codex.wordpress.org/Function_Reference/query_posts#Exclude_Categories_From_Your_Home_Page
Then in your page you just have to check the category of the Post where you are and replace "-3" by the category ID you want to avoid.
I'm trying query a custom post type for each comment with the dynamic field "comment_ID." I'm using the code below. This currently shows the comment ID, which I don't want, but does not show 'paid' as I would like.
<?php
$commID = comment_ID();
$args = array( 'post_type' => 'paidbriefs', 'meta_key' => 'Comment_ID', 'meta_value' => 'echo $commID', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo 'paid';
endwhile; ?>
</p>
I'm obviously doing something wrong with echoing the $commID variable as this does not show anything. If I change this to just $commID it returns 'paid' for every comment that has a Comment_ID meta, regardless of whether it matches the actual comment ID. Does anyone know how to fix this?
I think what you will need to do is a Loop, within a loop,
first loop to generate the standard loop, this will have your post info, comments etc.
within that loop you need to declare your comment_ID;
then from there, you setup another internal loop,
using the comment_ID for your custom field,
<?php
$args = array( 'post_type' => 'paidbriefs', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$commID = comment_ID();
$innerargs = array( 'post_type' => 'paidbriefs',
'meta_key' => 'Comment_ID',
'meta_value' => $commID,
'posts_per_page' => 10 );
$innerloop = new WP_Query( $innerargs );
while ( $innerloop ->have_posts() ) : $innerloop ->the_post();
echo 'paid Comment';
endwhile;
endwhile;
?>
untested though.
hopefully this will help,
<?php
$args = array( 'post_type' => 'ait-dir-item',
'meta_query' => array(
array(
'key' => 'location',
'value' => 'annapolis'
),
array(
'key' => 'item_tags',
'value' => 'non-marine'
)
),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title('<h3 class="entry-title">', '</h3>');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;?>
you can try this one