I had created this loop for getting the last added taxonomy category posts.Here is the code
$issue = get_terms('articles-tax','orderby=Desc&order=ASC');
$latest_edition = $issue[0]->slug;
$latest_edition = $issue[0]->term_id;
$postsart = get_posts(array(
'showposts' => -1,
'post_type' => 'articles',
'tax_query' => array(
array(
'taxonomy' => 'articles-tax',
'field' => 'term_id',
'terms' => $latest_edition)
)) ); foreach ($postsart as $mypost) { ?>
<div class="article_item"><div class="article_title"><?php echo $mypost->post_title?></div><div class="article_short_content"> <?php echo $mypost->post_excerpt ?> </div>
<div class="news_border"></div>
<div class="news_readmore">Read More</div>
</div>
So Now I cant get the permalink of the post, didnt know how to get from the array.Had done var_dump of $mypost, but cant manage how to get one item permalink.Help me to fix this pls!
use
$permalink = get_permalink($mypost->ID);
echo $permalink ;
It will give you posts' permalink
Adding "global post" function to the code seems to do the trick
like this:
$postsart = get_posts(array(
'showposts' => -1,
'post_type' => 'articles',
'tax_query' => array(
array(
'taxonomy' => 'articles-tax',
'field' => 'term_id',
'terms' => $latest_edition)
)) );
global $post;
foreach ($postsart as $post) {
setup_postdata($post);
?>
<div class="article_item"><div class="article_title"><?php echo $post->post_title?></div><div class="article_short_content"> <?php echo $post->post_excerpt ?> </div>
<div class="news_border"></div>
<div class="news_readmore">Read More</div>
</div>
Related
Some of my posts have a class name ‘disabled’ and I have to remove them from related posts because they have a style display: none so it inserts it but doesn’t display it.
How can I filter posts with a class name disabled and add them to 'posts__not_in' to avoid them?
<?php
$related = get_posts( array(
'numberposts' => 4,
'post__not_in' => array($post->ID)
) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>" class="card <?php if( get_field('public') == 'no') echo 'disabled' ?>">
<p><?php the_title();?></p>
</a>
<?php } wp_reset_postdata(); ?>
My recommendation would be to create a category and assign it to the posts. Then use a tax_query to filter out these posts from your get_posts(). Example below is Psuedo code:
$related = get_posts([
'post_type' => 'post',
'numberposts' => 4,
'post__not_in' => [$post->ID],
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'DISABLED_CATEGORY_SLUG',
'operator' => 'NOT IN'
],
],
]);
I've got a custom post type that I've sorted by date from an ACF date picker field. I need to be able to filter these results based on a custom taxonomy that I've got. I can't seem to work out where to add the tax_query array. Keeps breaking the site.
Taxonomy name is 'pre_job_status', term to filter by is 'survey-complete'
Currently I have the following code
<?php
// get posts
$posts = get_posts(
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'survey-complete' )
),
),
array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'survey_date',
'orderby' => 'meta_value',
'order' => 'ASC',
));
if( $posts ): ?>
<hr>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php $requestor = get_field('pre_job_requestor', $client->ID ); ?>
<?php $survey_site = get_field('survey_site', $client->ID ); ?>
<li>
<?php the_field('survey_date'); ?> - <?php the_title(); ?> - <?php echo $requestor[0]->post_title; ?> - <?php echo $survey_site[0]->post_title; ?>
</li><hr>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Your get_posts() query should look like the following:
// get posts
$posts = get_posts(array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'survey_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'survey-complete' )
),
),
));
The tax_query array should be added to the main query array.
I would like to get a pager in my page, can i create a pager with a for each or while without parameter like "posts_per_page" ?
I get term children of my taxonomy and i would like to display 3 by 3
My code :
$childGalerie = get_term_children(15,'wpmf-category');
foreach ( $childGalerie as $oneGalerie ) :
$maGalerie = get_term($oneGalerie, 'wpmf-category');
?>
<h2 class="info-galerie"><span></span><?php echo $maGalerie->name; > </h2>
<div class="galerie-photo">
<?php
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'attachment',
// 'post_status' => 'inherit',
'tax_query' => array(
array(
'taxonomy' => 'wpmf-category',
'field' => 'term_id',
'terms' => $oneGalerie
),
),
);
$media = get_posts( $args );
foreach ( $media as $post ) : setup_postdata( $post );
?>
<div class="wrapper-galerie">
<?php echo wp_get_attachment_image( $media->ID, 'galerie->photo' ); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
</div>
<?php
endforeach;
Can you help me please , ty :D
So I'm still getting the hang of WordPress Custom Post Types and how to integrate them into an existing template, but I have a quick question about whether the code I've come up with can be simplified?
I have multiple pages I need to call from one particular template, and I've added conditional statements to call each page and its corresponding custom post type code, but I'm essentially repeating most of the code for each conditional statement, so I was wondering if there was a way to simplify this code:
<div class="row">
<?php if (is_page(165)) {
$args = array(
'post_type' => 'restaurant',
'paged'=>$paged,
'orderby'=>'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'restaurant_category',
'field' => 'slug',
'terms' => 'sooke'
)
)
);
$restaurants = new WP_Query( $args );
if( $restaurants->have_posts() ) {
while( $restaurants->have_posts() ) {
$restaurants->the_post();
?>
<div class="col-sm-4 col-md-4">
<h1><?php the_title() ?></h1>
<?php the_content() ?>
</div>
<?php
}
}
else {
echo 'No Restaurants';
}
} ?>
</div>
<div class="row">
<?php if (is_page(12)) {
$args = array(
'post_type' => 'restaurant',
'paged'=>$paged,
'orderby'=>'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'restaurant_category',
'field' => 'slug',
'terms' => 'chilliwack'
)
)
);
$restaurants = new WP_Query( $args );
if( $restaurants->have_posts() ) {
while( $restaurants->have_posts() ) {
$restaurants->the_post();
?>
<div class="col-sm-4 col-md-4">
<h1><?php the_title() ?></h1>
<?php the_content() ?>
</div>
<?php
}
}
else {
echo 'No Restaurants';
}
} ?>
</div>
If there is a way to simplify this code so that I'm not repeating code again and again, I would really appreciate the help to do so, so that I can learn how and how not to create conditional statements with custom post type code in the future. Thanks in advance!
For Dk-Macadamia (updated code that results in white screen)
<?php $terms=array('165'=>'sooke','12'=>'chilliwack');
//now check it
if(has_term($terms))
{ ?>
<div class="row">
<?php
foreach($terms as $key=>$val)
{
if(is_page($key)) {
$args = array(
'post_type' => 'restaurant',
'paged'=>$paged,
'orderby'=>'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'restaurant_category',
'field' => 'slug',
'terms' => $val
)
)
); ?>
</div>
I think you are looking for has_terms(), you can try in in your code as:
Becouse you are working on custom template, so you can set a manually array something like this.
$terms=array('165'=>'sooke','12'=>'chilliwack');
//now check it
if(has_term($terms))
{ ?>
<div class="row">
<?php
foreach($terms as $key=>$val)
{
if(is_page($key)) {
$args = array(
'post_type' => 'restaurant',
'paged'=>$paged,
'orderby'=>'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'restaurant_category',
'field' => 'slug',
'terms' => $val
)
)
);
With the use of switch statements
switch ($slug_name)
case 1 : 'chilliwack'
my_func('chilliwack');
break;
and so on ....
// here is function
<?php
function my_func($slug) {
$args = array(
'post_type' => 'restaurant',
'paged'=>$paged,
'orderby'=>'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'restaurant_category',
'field' => 'slug',
'terms' => "$slug"
)
)
);
$restaurants = new WP_Query( $args );
if( $restaurants->have_posts() ) {
while( $restaurants->have_posts() ) {
$restaurants->the_post();
?>
<div class="col-sm-4 col-md-4">
<h1><?php the_title() ?></h1>
<?php the_content() ?>
</div>
<?php
}
}
else {
echo 'No Restaurants';
}
}
?>
</div>
Following is my wordpress query which I am using to display all the posts but the query is not displaying the post's tags, Also kindly let me know how to modify the following query so it displays the posts from any specific category.
<?php
$wp_query2 = null;
$wp_query2 = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'caller_get_posts'=> 0 ));
while ($wp_query2->have_posts()) : $wp_query2->the_post();
?>
<?php the_date(); ?>
<br />
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php
wp_reset_query();
?>
You may try this
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'category1', 'category2' ) // replace these
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array( 'tag1, tag2' ) // replace these
)
)
);
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post();
// ...
the_content();
the_tags(); // display tags
endwhile;
Check WP Query and the_tags.