How to Separate Wordpress Query Result by Taxonomy with Heading? - wordpress

I have a wordpress custom post type of "awards", which has a custom taxonomy of "awardyear." I'm trying to write a query that will list them all out, but group them by year, and have the year in a heading. For example all of the awards from the year 2010 would be grouped together & have "2010" as the header. Is this even possible? I'm able to get them all listed out, however, it's currently listing them all out under each year. Here is my current query:
$taxonomy = 'awardyear';
$queried_term = get_query_var($taxonomy);
$terms = get_terms($taxonomy, 'slug='.$queried_term);
$args = array(
'post_type' => 'awards',
'posts_per_page' => -1 ,
'awardyear' => $term->name,
'order_by' => 'awardyear',
'order' => 'ASC'
);
if ($terms) {
foreach($terms as $term) {
echo '<span class="award_year">' . $term->name . '</span> ';
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li style="list-style: none;">';
echo '<a href="';
the_permalink();
echo '">';
the_title();
echo '</a>';
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
}
}
Example output would be:
2010
Award Example Number 1
Award Example Number 2
2011
Award Example Number 1
Award Example Number 2
Award Example Number 3
Award Example Number 4
2012
Award Example Number 1
Award Example Number 2
Award Example Number 3

Your problem is that $args['awardyear'] gets set, once, before $term is defined and will therefore be undefined itself. Remove it from the definition of $args and put $args['awardyear'] = $term->name; at the top of your foreach loop. That way it will be set correctly on each past.
Please note that this sort of use of query_posts() is generally frowned upon; see the Codex entry for that function.

You may want to consider doing it a different way as post meta would be a better choice on actually accomplishing what you want effectively.
Although regardless if you really want to use the taxonomy you will need to do it based on a query that takes the taxonomy columns and you can display it from there.
Reference: http://scribu.net/wordpress/sortable-taxonomy-columns.html

Related

Wordpress standard loop: additional post count with parameters

I am altering a template file of the Search & Filter Wordpress Plugin to show additional post counts.
The plugin uses the standard Wordpress loop to query posts by certain parameters. The number of posts can be counted by using found_posts.
Now I want to display a second post count that takes additional parameters into account, eg. post_status. I have to stick to the regular WP loop to keep the query from the Plugin intact.
Something like this in the commented line:
if ( $query->have_posts() ) {
echo $query->found_posts 'posts found';
// echo $query->found_posts(array('post_status=>'private') 'private posts found';
while ($query->have_posts()) {
$query->the_post();
the_title();
}
}
The code works fine, except for the commented part which obviously doesn't work.
Is there a way to add another post_count with additional parameters to the standard loop?
Thanks
Georg
You could not add like this echo $query->found_posts(array('post_status=>'private') but before your loop just use below code to count posts by status. in your case you want to count posts by status private so add below code.
$args = array('post_type' => 'your_post_type_name','post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash')
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo $query->found_posts 'posts found';
// echo $query->found_posts(array('post_status=>'private') 'private posts found';
$count_posts = wp_count_posts('post');
$private_posts = $count_posts->private;
echo $private_posts. 'private posts found';
while ($query->have_posts()) {
$query->the_post();
the_title();
}
}
similarly if you want to show count for publish or draft posts you can add below code.
$publish_posts = $count_posts->publish;
$draft_posts = $count_posts->draft;
you could count any other posts by its status is well.

How to get the id out of the get_categories objects

I want to display the id's of my categories using the get_categories function.
I have looked on google and stackexchange and foud these 2 methods
$categories = get_categories(
array(
'orderby' => 'name',
'order' => 'ASC'
)
);
foreach( $categories as $category ) {
echo '<p>' .'Category id'. $category->cat_id . '</p>';
// the method above and below are the ones i found
// but both the id and cat_id give no results
echo '<p>' .'Category id'. $category->id . '</p>';
}
I know i can use:
echo get_cat_ID( $category->name );
But i want i want it clean like the code above.
Anyone has any ideas why this is not working?
I believe that "cat_ID" is a pseudonym of "term_id" and can only be used with a limited set of functions. You mention get_cat_ID and its source shows it returns term_id.
Your existing code may work if you change cat_id to cat_ID - but I'd use term_id instead (it certainly works for me with the "similar" get_the_category function)

How to add taxonomy term to title tag in WordPress?

I inherited a website that includes a custom post type (Cities). Due to some wonky url rewrites and redirects, SEO plugins won't work on those pages.
This custom post type has a taxonomy (States) and I need to insert the state name into the title tag in my header.php. I've tried the following, to no avail:
<title>Log Cabin Rentals in <?php single_term_title(); ?> - Find Your Dream Vacation Rental in <?php single_term_title(); ?></title>
I also tried:
<?php
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
echo '<title>Cash for Gold Near ' . $term->name . ' - Find Gold Buyers Near ' . $term->name .'</title>';
echo '<meta name="description" content="Looking for cash for gold near ' . $term->name . '? Our cash for gold directory helps you find reputable local gold buyers and pawnshops near ' . $term->name . ', as well as info on how to earn the most cash for gold.">';
?>
What am I doing wrong?
Many thanks!
Cynthia
Give this a shot instead:
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
echo $term->name;
If that echoes out your taxonomy name, you could replace the echoes in your title tag with $term->name
Solution One:
get_terms() - Retrieve the terms in a given taxonomy or list of taxonomies.
You can fully inject any customizations to the query before it is sent, as well as control the output with a filter.
The ‘get_terms’ filter will be called when the cache has the term and will pass the found term along with the array of $taxonomies and array of $args. This filter is also called before the array of terms is passed and will pass the array of terms, along with the $taxonomies and $args.
get_terms returns an array of objects. You cannot echo an array, if you do, you will just get Array(). What you can do is print_r($array) or var_dump($array) to see the data it contains.
$taxonomy = 'shirt';
$args=array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC'
);
$tax_terms = get_terms( $taxonomy, $args );
foreach ( $tax_terms as $tax_term ) {
echo $tax_term->name;
}
Solution Two:
You can use the function called get_taxonomies() in order to query out the taxonomy that you need.
Syntax:
<?php get_taxonomies( $args, $output, $operator ) ?>
Example:
This example uses the 'object' output to retrieve and display the taxonomy called 'genre':
<?php
$args=array(
'name' => 'genre'
);
$output = 'objects'; // or names
$taxonomies=get_taxonomies($args,$output);
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
echo '<p>' . $taxonomy->name . '</p>';
}
}
?>

wordpress custom post title viewer development

I want to create something like this in wordpress, so that in a wordpress page I can show users the post titles which are linked to their post links and also the date of those posts.
what kind of code should I use in wordpress? I am new to wordpress coding. so please give me some sample code so that I can use.
also I want to choose posts from one special category. how can I do that? is there any wordpress plugin which does this?
pagination links are also important to have.
Thanks
I would look into WP_Query. It may take a little while to get used to it but it lets you echo all the data you want. I use it for almost everything now.
In the $args array you define what you want to query (such as posts from a specific category, how many, in what order), and then from there you can pull the title, the date published, the attachments, the excerpt, featured image, etc.
<?php
// The Query
$args = array(
'numberposts' => '5',
'category_name' => 'my-category', // the slug
'offset' => '0',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
?>
The great part about WP_query is that you can use it to have multiple loops on 1 template. This would replace the normal loop in the template.
For date, you would use the_date() or get_the_date(). So you might do an
echo '<p><span class="date">' . get_the_date() . '</span> <a class="my-title" href="' . get_the_title() . '">' . get_the_title() . '</a></p>';
There's more than one way to do this (and almost everything) in WordPress, so it depends on what you're doing and what else is going on in the code. There's query_posts() and wp_get_recent_posts()... really there's too many to mention. I'd post more links but my rep isn't high enough to.
I'd start looking into WP_Query as you can pull whatever data you want from that and format it to your heart's content.
Anyway, hope this well help you.

Print WooCommerce products list sorted by SKU

My client has asked "I would like to be able to print a listing of all products by SKU (sorted alphanumerically) with description, and by description (alpha) with the associated SKU." I think the closest I could get for him is to have the SKU sorted alphanumerically but not both, though I could be wrong.
Looking at this question: Woocommerce: get a list for all sku product, I wonder if I could build a table instead of a list and pull the description in next to the SKU?
Here's the code I'm thinking of based on the link above (without sorting):
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
query_posts( $args );
if( have_posts() ):
echo '<table>';
while ( have_posts() ) : the_post();
echo '<tr><td>'. $product->get_sku() . ' - ' . $product->get_title() . '</td><td>' . $product->get_description() . '</td></tr>';
endwhile;
echo '</table>';
endif;
I don't think the description is the correct way to call it, but I didn't see one to call in the short description.Also, I'm not sure how I would have it sort by SKU. My PHP knowledge is limited.
Even if that code worked, I'm not sure where I would put it to call this in. Can I make it on an admin page or would it be better to create a regular page and use a different template and put this directly into the PHP of that template file?
Any suggestions would be great!

Resources