Get Posts from custom taxanomy - wordpress

I want to get posts with custom taxonomy, you can check the code below ppctrainings is the custom taxonomy category and 94 is the id of category.
<?php
$count = 0;
// this is the custom taxonamy ppctrainings the id is 94
$posts = get_posts('ppctrainings=94&numberposts=10');
foreach($posts as $post) {
if($count == 3){
echo '</tr><tr >';
$count = 0;
}
$count++;
?>

<?php
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'sliders_category', // taxonomy category
'field' => 'id',
'terms' => '2' // category id
)
),
'post_type'=>'sliders', //name of post type
'order'=>'ASC',
'posts_per_page'=>10
);
query_posts($args);
while ( have_posts() ) : the_post();
?>
// your code for display title and content
<?php
endwhile;
wp_reset_query();
?>

Use the below code for getting the custom post type data.
$args = array(
'posts_per_page' => '-1',
'post_type' => 'blog',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'post_date');
$the_query = new WP_Query($args);
while ($the_query->have_posts()) {
$the_query->the_post();
// Write your code here
}

There are many ways you can query the WordPress database
pre_get_posts action
query_posts()
the WP_Query

Related

Select post with specific group - subfield value

in wordpress I have custom post type 'referenzen'. This post type has Custom fields(ACF) 'Referenzen-buildin-type' Group with subfield 'building-type' which is checkbox. I do not know how to select posts with specific building type. This is not working for me :
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'referenzen-building-types_building-type',
'value' => '"Museen"',
'compare' => 'LIKE'
)
)
));
Any idea? Thanks
Please have a look below:
<?php
//args
$args = = array (
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
//query
$the_query = new WP_Query( $args );?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
/* do something */
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I hope it would help you out.
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'referenzen_building_types_building_type',
'value' => 'Museen',
'compare' => 'LIKE'
)
)
));

need to print post filtered by my current taxonomy's page?

i need to print all my post filtered by my current taxonomy's page.
which is the query with which I can have all these posts filtered for the current page taxonomy?
i'm trying to do a widget dynamic to use into more pages with different categories.
i hope someone can help me . thx :)
i'm trying with this code but doesn't work...
<?php
$args=array(
'post_type' => 'post,
'post_status' => 'publish',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
solved:
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array (
'taxonomy' => $taxonomyName,
'field' => 'slug',
'terms' => $term_slug,
)
),
));
?>

Show categories not in gallery post format

I want to show all blog categories that aren't of type gallery. My code almost works but if I have 2 posts in the same category the category is shown twice ie:
If I create a category called 'news' and add 2 non-gallery posts, it shows up as:
news
news
instead of just
news
<?php
$galleryPosts = new WP_Query(array(
'post_type' => 'post',
'order' => 'ASC'
));
?>
<?php if ( $galleryPosts->have_posts() ) : ?>
<?php while ( $galleryPosts->have_posts() ) : $galleryPosts->the_post(); ?>
<?php if(!has_post_format('gallery')) {
the_category();
}
?>
<?php endwhile; ?>
<?php endif; ?>
To suppress non-gallery posts, try selecting only those posts initially and then remove the has_post_format('gallery') check:
$posts = new WP_Query(
array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-gallery'
),
'operator' => 'NOT IN'
)
)
)
);
Then in PHP:
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
the_category();
}
}

How do I query to retrieve custom post title by tag name in wordpress

I want to retrieve custom post title by tag name.I have tag name and i want to retrieve title.How can i do this.
<?php
$tagname=get_post_meta(get_post()->ID, 'campaign_tag', true);
echo $tagname;
$args = array(
'post_type' => 'campaign',
'posts_per_page' => 20,
'tag' => $tagname,
);
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<h5><?php the_title(); ?></h5>
<?php endwhile; wp_reset_query();
?>
For CPT (custom post type) You should use tax_query.
Like that:
$args = array(
'post_type' => 'campaign',
'tax_query' => array(
array(
'taxonomy' => 'tax_name',
'field' => 'slug',
'terms' => 'bob',
),
),); $query = new WP_Query( $args );
Refer: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

How to Display Terms for all posts in Current Archive or Query in wordpress

I have custom post types with select option.In select option I got all the taxonomy categories and on change of select it displayes all posts of post type but when I go to single page it displayes only this current post not all from this category. I tried this code
$term = $wp_query->queried_object; but it displayes only the current post of this category not all.
How can I get all posts from the current taxonomy category.
Seems I got the solution.Here is my final code
$terms = wp_get_post_terms( $post->ID, 'course_type' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'courses',
'tax_query' => array(
array(
'taxonomy' => 'course_type',
'field' => 'slug',
'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<ul>
<?php while (have_posts() ) : the_post(); ?>
<li><? php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
} // end if($terms)

Resources