Show posts from custom taxonomy - wordpress

Update 2
Adding name as field instead of the slug and adding the_title() just give me an echo of the page title...
$args = array(
'post_type' => 'feestlocaties',
'showposts' => '3',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'name',
'terms' => the_title(),
),
),
);
Update Jonnhyd23's code worked like a charm!! Thanks!
Is there a way you can make the terms dynamic? Like the title is Amsterdam can I do something like 'terms' => '<?php the_title(); ?>' or something like that?
I've been going at this for the last couple of hours. Maybe someone here can help me?
I want to show specif posts from a custom taxonomy in a loop.
This is the situation:
custom taxonomy: feestlocaties
And the the posts i want to show have Amsterdam selected (checked) (like categories).
Code i tried:
<div id="main-filter">
<!-- Start the Loop. -->
<?php $args = array(
'post_type' => 'feestlocaties',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'slug',
'terms' => 'amsterdam',
),
),
); ?>
<?php $query = new WP_Query( $args ); ?>
<?php if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post(); ?>
<!-- Test if the current post is in category 3. -->
<!-- If it is, the div box is given the CSS class "post-cat-three". -->
<!-- Otherwise, the div box is given the CSS class "post". -->
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="container post-item">
<div class="col-sm-3 no-padding">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(400,355)); // Declare pixel size you need inside the array ?>
<?php endif; ?>
</div>
<div class="col-sm-9 no-padding">
<h1 class="overzicht"><?php the_title(); ?></h1>
<?php html5wp_excerpt('html5wp_index'); ?>
<div class="col-sm-12 no-padding loop-overzicht">
<?php $prijs = get_Field('vanaf_prijs'); ?>
<?php $pers = get_Field('aantal_personen'); ?>
<?php $time = get_Field('tijdsduur'); ?>
<ul class="loop-opsomming text-right">
<li><?php echo '<i class="fa fa-euro"></i>Vanaf ' . $prijs . ' p.p.' ?></li>
<li><?php echo '<i class="fa fa-group"></i>Vanaf ' . $pers . ' personen' ?></li>
<li><?php echo '<i class="fa fa-clock-o"></i>Vanaf ' . $time . ' uur' ?></li>
</ul>
</div>
</div>
</div>
</a>
<?php wp_pagenavi(); ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
But nothing is showing. Any help would be great. Thanks!

Are you just showing the arguments you're using for WP_Query, or is this all of your code? Try using the tax_query parameter.
$args = array(
'post_type' => 'your_post_type',
'tax_query' => array(
array(
'taxonomy' => 'feestlocaties',
'field' => 'slug',
'terms' => 'amsterdam',
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post();
//execute code
endwhile; endif; wp_reset_postdata();

So, I fiddeld around abit and this is the code that works for me. Cheers Johnnyd23
<?php $args = array(
'post_type' => 'feestlocaties',
'showposts' => '3',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'name',
'terms' => get_the_title(),
),
),
); ?>
<?php $query = new WP_Query( $args ); ?>
<?php if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post(); ?>
This will make the title the post dynamicly in the WP_Query.

Related

Alternatives to using get_pages in WordPress to get children page data

I am using get_pages to fetch some data from the children pages of a parent in WordPress (like a custom loop) - but it doesnt work when trying to fetch some data as set by the Advanced Custom Fields plugin for some strange reason... Is there an alternative / better way to acheive what I want? Code below works apart from fetching the ACF field called 'job_title'.
<?php
$args = array(
'parent' => $post->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish',
'sort_order' => 'DESC',
'sort_column" => "post_name',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$pages = get_pages($args); ?>
<div class="childrenFetchedLoopWrapper">
<?php foreach( $pages as $page ) { ?>
<div class="feedItemWrapper wpb_animate_when_almost_visible wpb_fadeInUp fadeInUp" style="background-image: url('<?php echo get_the_post_thumbnail_url($page->ID, 'full'); ?>')">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<img src="/wp-content/themes/salient-child/images/aspectTrans.png" alt="*" title="*" />
<h3><?php echo $page->post_title; ?></h3>
<p><?php the_field('job_title'); ?></p>
</a>
</div>
<?php } ?>
</div>
Replace <?php the_field('job_title'); ?> with <?php the_field('job_title', $page->ID); ?>.
OR
You can use WP_Query for alternative solution.
or you can also get acf value using get_post_meta();
Try Out this code. I hope it helps.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post();
$id = get_the_ID(); ?>
<p><?php the_field('job_title', $id); ?></p>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>

how display post on subcategory

I want to display posts in the subcategory but don't know what to do. But this code does not show the post in the category but the entire post in the category. Can you help me?
<?php
$query = new WP_Query( array(
'post_type' => 'hotcheck',
'topics__in' => array($cat->term_id),
) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
Please try below code
$args = array(
'post_type' => 'hotcheck',
'tax_query' => array(
array(
'taxonomy' => 'category', // Add your taxonomy name here
'field' => 'term_id',
'terms' => $cat->term_id,
),
),
);
$query = new WP_Query($args);
Hope its work for you Thanks!

WP_Query post__not_in is not working. Ignores the array

I've been through other posts on here and elsewhere and compared my code to working examples I've done previously, but I can't find what the issue is. I;m using the following query to grab a featured article and store its ID:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 564,
);
$featured_latest = new WP_Query($args);
$fid = array();
if( $featured_latest->have_posts() ) : while( $featured_latest->have_posts() ) : $featured_latest->the_post(); ?>
<?php $fid[] = get_the_ID(); ?>
<div class="top-featured">
<?php if( has_post_thumbnail() ) { ?>
<?php $image = get_the_post_thumbnail_url(get_the_ID(),'large'); ?>
<?php } else { ?>
<?php $image = get_stylesheet_directory_uri() . '/assets/images/blog/no-article-image.jpg'; ?>
<?php } ?>
<a class="article-link lazy" href="<?php the_permalink(); ?>" data-src="<?php echo $image; ?>">
<div class="text">
<h2><?php the_title(); ?></h2>
<?php
$date = get_the_date();
$cdate = date( 'c', strtotime($date) );
?>
<time datetime="<?php echo $cdate; ?>"><?php echo $date; ?></time>
<div class="excerpt">
<?php echo get_excerpt(140); ?>
</div>
<span class="fake-link">Read article</span>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
Then lower down the page I use this query:
<?php
if( !empty( $fid ) ){
$fid = $fid;
} else {
$fid = array();
}
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 399,
'post__not_in' => $fid,
);
$top_reviews = new WP_Query($args);
if( $top_reviews->have_posts() ) : ?>
<div class="top-articles top-reviews">
<h2>Top product reviews</h2>
<div class="row">
<?php while( $top_reviews->have_posts() ) : $top_reviews->the_post(); ?>
<div class="col-12 col-md-4">
<?php if( has_post_thumbnail() ) { ?>
<?php $image = get_the_post_thumbnail_url(get_the_ID(),'large'); ?>
<?php $imgid = get_post_thumbnail_id( get_the_ID() ); ?>
<?php $alt = get_post_meta( $imgid, '_wp_attachment_image_alt', true); ?>
<?php } else { ?>
<?php $image = get_stylesheet_directory_uri() . '/assets/images/blog/no-article-image.jpg'; ?>
<?php $alt = 'No article image'; ?>
<?php } ?>
<a class="article-link" href="<?php the_permalink(); ?>" data-src="<?php echo $image; ?>">
<img class="lazy" data-src="<?php echo $image; ?>" alt="<?php echo $alt; ?>">
<div class="text">
<h3><?php the_title(); ?></h3>
<?php
$date = get_the_date();
$cdate = date( 'c', strtotime($date) );
?>
<time datetime="<?php echo $cdate; ?>"><?php echo $date; ?></time>
<div class="excerpt">
<?php echo get_excerpt(140); ?>
</div>
<span class="fake-link">Read article</span>
</div>
</a>
</div><!-- col -->
<?php endwhile; wp_reset_postdata(); ?>
</div><!-- row -->
</div>
<?php endif; ?>
$fid prints as an array with one item inside, but the second query does not exclude the post of that ID. I'm sure there's something glaringly obvious that I'm missing, but I can't for the life of me find it!
May be some other plugin or theme features overrides the array? var_dump and try what are the query_vars in the WP_Query object. then you can identify it. Or simply deactivate all the plugins and activate it one by one. then you can identify surely!
if any plugin overrides it, they should use a hook to do it. so search and see the priority of the hook. and then just you override it with high priority hook. example below.
add_action('pre_get_posts', 'your_callback_function_name', 999)
function your_callback_function_name($query){
$overridden_array = $query->get('post__not_in', array());
$overridden_array[] = $your_f_id;
$query->set('post__not_in', $overridden_array);
}
Thats all. Just rename those functions and variables as your wish.
You need to pass array in this format array('162','3074')
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'post__not_in' => array('162','3074')
);
$top_reviews = new WP_Query($args);
Its working fine for me.. you can try in same way.
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored.
Change post__not_in value into Array.
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 399,
'post__not_in' => array('YOUR_POST_ID1','YOUR_POST_ID2'),
);
For more help see this link : Click Here
When printing out the WP_Query resuls, I found that the following was happening:
WP_Query Object ( [query] => Array ( [post_type] => post [posts_per_page] => 3 [orderby] => date [order] => desc [post_status] => publish [cat] => 399 [post__not_in] => Array ( [0] => 8117 ) ) [query_vars] => Array ( [post_type] => post [posts_per_page] => 3 [orderby] => date [order] => DESC [post_status] => publish [cat] => 399 [post__not_in] => Array ( [0] => 7990 )
It was suggested to me by dineshkashera that the pre_get_posts() filter may have been the cause. As I was not using this myself, I troubleshooted for plugin conflicts by deactivating all those that were not regular ones I used. After reactivating them one at a time, I found that the culprit was the Woocommerce Point of Sale plugin. I have deactivated this plugin for now and will seek a solution from the developer.

Custom Post Type showing even when taxonomy is blank

I am using Custom Post Type to get its contents by using Custom Taxonomy in different pages.
However, the contents are being displayed even if the post type has no taxonomy.
P.S. I am using ACV as well to get the fields of Taxonomy
Here's the code:
<?php if( get_field('review_category_name') ):
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
endif; ?>
<?php $args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args ); ?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
Am I missing anything here?
$taxonomy_slug is a variable to get the custom post type slug.
Wrap everything in the get_field('review_category_name') if check:
<?php if( get_field('review_category_name') ): ?>
<?php
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
$args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args );
?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
PS your example was missing the endwhile

WordPress - custom post type pagination

I have a custom post type (question) related to a custom taxonomy (support)
In my-theme/taxonomy.php I have this code:
<?php
$taxonomy = get_queried_object()->taxonomy;
if ($taxonomy == 'support')
{
get_template_part('template/support/categories');
exit;
}
wp_safe_redirect(site_url('/'));
exit;
?>
Means I target a specific template file for "support" taxonomy.
In my taxonomy template file I make a custom query:
<?php $current_category = get_term_by('id', get_queried_object()->term_id, 'support'); ?>
<?php $questions = new WP_Query(array(
'post_type' => array('question'),
'post_status' => 'publish',
'posts_per_page' => 5,
'posts_per_archive_page' => 5,
'paged' => ((get_query_var('page')) ? get_query_var('page') : 1),
'nopaging' => false,
'tax_query' => array(
array(
'taxonomy' => 'support',
'terms' => array($current_category->term_id)
)
),
'orderby' => 'menu_order',
'order' => 'ASC'
)); ?>
<?php if ($questions->have_posts()): ?>
<ul class="list normalize">
<?php while ($questions->have_posts()) : $questions->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<div class="nav-previous">
<?php next_posts_link( __('Previous', 'my-theme'); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __('Next', 'my-theme'); ?>
</div>
<?php wp_reset_postdata(); ?>
I have about 11 posts, page 1 shows the 5 first posts, but the problem is that no pagination is shown.
Any idea?
Thank you
You can try this pagination plugin
http://wordpress.org/plugins/wp-pagenavi/
And add this code in your template
<?php if (!function_exists('wp-pagenavi')) { wp_pagenavi(); } else { ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div><?php } ?>
I tried Your code and with my taxonomy
<?php $questions = new WP_Query(array(
'post_type' => array('studies'),
'post_status' => 'publish',
'posts_per_page' => 15,
'paged' => get_query_var('paged'),
'nopaging' => false,
'tax_query' => array(
array(
'taxonomy' => 'studies',
'terms' => 11
)
),
'orderby' => 'menu_order',
'order' => 'ASC'
));
?>
<div class="grid9">
<div class="entry-content">
<?php if ($questions->have_posts()): ?>
<ul class="list normalize">
<?php while ($questions->have_posts()) : $questions->the_post(); ?>
<li>
<h3><?php echo get_the_title(); ?></h3>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php
wp_pagenavi( array( 'query' => $questions ) );
?>
This works fine ,Don't forget to use wp-pagenavi plugin.
Put this code above the "endif;" and just below the end of while loop.
<div class="nav-previous">
<?php next_posts_link( __('Previous', 'my-theme'); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __('Next', 'my-theme'); ?>
</div>
<?php wp_reset_postdata(); ?>
Hope this helps you.
Ussually this works for me:
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $questions->max_num_pages
) );
Notice the question variable. :-) After you use this, go to Settings->Permalink and Save twice.
You can find a bigger tutorial here with the same example.

Resources