WordPress Get Terms and Posts Query is Breaking Other Posts - wordpress

I have the following query, which is designed to show a list of terms in a taxonomy, and under each term, a list of posts assigned to that term.
This appears in a sidebar on the left hand side of my single.php posts page. In the main area of the page, the actual single post is meant to display.
However, instead of displaying the current single post, it is only displaying the TITLE for the most recent single post.
Here is my query:
$terms = get_terms('benefit-cats');
echo "<ul>";
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug);
$query = new WP_Query ($wpq);
echo "<li class=\"list-item\">".$term->name.""; ////
echo "<ul class=\"children\">";
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
echo "</ul></li>";
}
echo "</ul>";
I have tried adding reset query to the code, to no avail. I have identified that it is this specific section that is causing the problem:
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
What exactly am I doing wrong here? I have been trouble shooting this for 30-40 minutes now without any success.
Would appreciate an explanation of my error.

try to use
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
echo "<li>".the_title()."</li>";
<?php endwhile; endif; wp_reset_query(); ?>
in place of
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
hope this will work fine for you

Your arguments for WP_Query aren't well formed.
Here's how it should be for taxonomy (snippet taken from the Codex ):
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob'
)
)
);
After you get your arguments right, you should loop over the results:
while( $query->have_posts() ):
$query->the_post();
// It is now that get_permalink() will work
endwhile;

Related

How to get taxonomy in a div

I want to add a taxonomy in my div with the different other fields, but I'm new to wordpress and O don't find how doing this, if someone can help me please ?
<?php/* Template Name: Archive Projets */ ?>
<?php get_header(); ?>
<?php
$posts = get_posts(array(
'posts_per_page' => 4,
'post_type' => 'projects'
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<div>
<?php the_title(); ?>
<div><?php the_field('url')?></div>
<div><?php the_field('')?></div> /* HERE I WANT TAXONOMY */
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Here's a code that will get the taxonomies of the current post and display them in a list of insert this code under <div><?php the_field('url')?></div>
// Get all terms
$terms = get_the_terms( $post->ID , array( 'Taxonomy_name') );
// Dispaly the taxonomies, if there one or more.
foreach ( $terms as $term ) {
echo '<div>' . $term->name . '</div></br>';
}
Make sure to change the 'Taxonomy_name' with your taxonomy name. let me know if I got you wrong or if you need any help with the code.
A quick google search brings up get_terms().
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
foreach($terms as $current_term) {
//You can now loop through them and get the ones you want, display them all, or whatever else it is you want to do. Note: each $current_term is an object of type WP_Term (or error if there are no results).
}

WordPress Nested Loops

I've got some code (see below) from an online tutorial that displays an alphabetical list of Category names and then,
underneath each category, a list of post titles for that category.
It works, but I want the post titles to also be displayed alphabetically. At present it's only category names that are alphabetical - see image:
I've done some research online and I think I may need to set up a 'nested loop' - but I have no idea how to edit my code to do this.
Hoping someone can show me how to edit code to get both category names AND post titles to display alphabetically.
This is the code I'm using:
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'orderby' => 'term_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
We can add the orderby argument with title.

Returning the name of a category in Wordpress

I have a loop for a custom post type an all is working well, except I cannot return the actual category name that the post has assigned to it.
I have the following code:
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC'
));
if($posts) {
foreach($posts as $post) {
setup_postdata( $post );
?>
<div>
<div class="mix <?php get_cat_name( ); ?>" ><img src="<?php the_field('portfolio_image'); ?>" alt=""></div> <!-- ACF -->
</div>
<?php
}
}
wp_reset_postdata();
?>
This is just one of the WP Codex functions I have tried including tring all with echo. I imagine my problem is something else?
Many thanks in Advance.
According to the codex an id is required for this method (https://codex.wordpress.org/Function_Reference/get_cat_name). Leaving out the id will not work.
One solution would be to use get_the_category($postId) to retrieve the category for your post (https://codex.wordpress.org/Function_Reference/get_the_category). You can leave out the postId here
post_id
(integer) (optional) The post id.
Default: $post->ID (The current post ID)
Note that the method returns an object - not the category name. You can try the following code to see if it's working:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
try this code
<?php $terms = get_the_terms($post->ID, 'TAXONOMY' );
if ($terms && ! is_wp_error($terms)) :
$term_slugs_arr = array();
foreach ($terms as $term) {
$term_slugs_arr[] = $term->name;
}
$terms_slug_str = join( " ", $term_slugs_arr);
endif;
echo $terms_slug_str;?>
Replace your code
from
get_cat_name( );
to
$categories = get_the_terms($post->ID,'custom-texonomy-name');
$cat = key($categories);
echo $categories[$cat]->name;

wordpress and future posts

I need to show future posts in all posts list on page.
my code
$args = array('posts_per_page' => 10,
'category' => $category->cat_ID );
?>
<?php $posts = get_posts($args); ?>
what arguments I can to write for showing all posts from category with future posts?
And it my archive page code, what should to show posts list from category
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
this solution mast to show future and past posts too
You should specify the post_status to be publish and future in your $args, for example:
$args = array(
'posts_per_page' => 10,
'category' => $category->cat_ID,
'post_status' => array(
'publish',
'future'
)
);
To get the posts with these args, use (for example):
$posts = get_posts($args);
To display these posts, use (for example):
foreach( $posts as $p ) {
// display the post title
echo '<h2>' . apply_filters('the_title', $p->post_title) . '</h2>';
// display the post date
echo '<h4>' . date('F jS, Y', strtotime($p->post_date)) . '</h4>';
// display an excerpt of the post
echo wp_trim_excerpt($p->post_content);
}
An alternative is to use query_posts / WP_Query to get these posts. In this case, to display the posts, you would use a loop, similar to the one in your question. Such example can be seen here: http://codex.wordpress.org/Class_Reference/WP_Query#Standard_Loop

wordpress - ordering multiples post types

i'm trying to order multiples post types in a page
my solution below is working but the posts are not showing by type after type exactly
<?php $args = array_merge( $wp_query->query,
array( 'post_type' =>array( 'editorial','video','portfolio' ),'posts_per_page=-1','orderby'=>'post_type') );
}
query_posts( $args );
$postType='';
if (have_posts()) : while (have_posts()) : the_post();
$PT = get_post_type( $post->ID );
if($postType != $PT){
$postType = $PT;
echo '<p class="clearfix"><h1>All posts of type : '.$postType.'</h1></p>';
}
?>
<?php
if($postType == 'editorial'){ ?>
<?php echo $postType; ?>
<?php }elseif($postType == 'video'){ ?>
<?php echo $postType; ?>
<?php }elseif($postType == 'portfolio'){
<?php echo $postType; ?>
}
?>
and it output like that:
All posts of type : editorial
editorial
editorial
editorial
All posts of type : video
video
video
All posts of type : editorial //problem
editorial
All posts of type : portfolio
portfolio
portfolio
portfolio
portfolio
thank you in advance
According to the Codex, post_type isn't one of the allowed values for the orderby parameter.
One way around your problem would be to use the posts_orderby filter, something like this:
function order_posts_by_type($original_orderby_statement) {
global $wpdb;
return $wpdb->posts .".post_type ASC";
}
add_filter('posts_orderby', 'order_posts_by_type');
$custom_query = new WP_Query( array(
'post_type' =>array( 'editorial','video','portfolio' ),
'posts_per_page' => '-1',
'orderby'=>'post_type',
'order' => 'ASC'
) );
remove_filter('posts_orderby', 'order_posts_by_type');
FWIW, I'd suggest changing 'posts_per_page=-1' to 'posts_per_page' => -1 to be consistent with your other arguments.

Resources