Here is the situation, I have a custom tax called Skill. I want to be able to display posts only with skill set as Japanese from English.
I am trying to learn how to use pre_get_posts hook to modify my get_posts query. Here is my example, however I land with the error:
Notice: Undefined variable: postdata
This is what I have tried based on research:
add_filter( 'pre_get_posts', 'wpshout_fundraiser_recent_posts' ); function wpshout_fundraiser_recent_posts( $query ) { // Fetch only posts tagged with "Japanese from English" $taxquery = array( array( 'taxonomy' => 'Japanese from English', 'field' => 'skill', 'terms' => array( 'skill' ), ) ); $query->set( 'tax_query', $taxquery ); I am sure something is wrong with the above query, which I don't fully understand. Any help and please explain what each field of the array is for if possible.
4 Answers
Answers 1
Try this below code this should work,
add_filter( 'pre_get_posts', 'wpshout_fundraiser_recent_posts' ); function wpshout_fundraiser_recent_posts( $query ) { $posts_array = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'Your Post Type Name', 'tax_query' => array( array( 'taxonomy' => 'Japanese from English', 'field' => 'skill', 'terms' => array( 'skill' ), ) ) ) ); return $posts_array; } Answers 2
Here you can check:
$args = array( 'posts_per_page' => 5, 'post_type' => 'Post Type Name', 'tax_query' => array( array( 'taxonomy' => 'category_taxonomy', 'field' => 'slug', 'terms' => "Category Name" ))); $query = query_posts( $args ); while (have_posts()) : the_post(); the_content(); endwhile; Answers 3
$posts_array = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'post_type_name', 'tax_query' => array( array( 'taxonomy' => 'taxonomy-name', 'field' => 'term_id', 'terms' => $cat->term_id, ) ) ) ); Answers 4
You can try this below code :
$custom_args=array( 'post_type' => "Your Post Type Name", 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> -1, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'tax_query' => array( array( 'taxonomy' => 'Your taxonomy slug', 'field' => 'slug', 'terms' =>"'Your Category Name" ) ), 'orderby' => 'id', 'order' => 'ASC' ); $custom_my_query = null; $custom_my_query = new WP_Query($custom_args); $custom_my_total_count = count($custom_my_query); if( $custom_my_query->have_posts() ) { while ($custom_my_query->have_posts()) : $custom_my_query->the_post(); ?> <a href="<?php echo get_permalink();?>"><?php echo get_the_title($post->ID);?></a> <?php endwhile; } wp_reset_query($custom_my_query); // Restore global post data stomped by the_post(). }
0 comments:
Post a Comment