wordpress custom post title viewer development - wordpress

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.

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)

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!

Advanced custom field (wordpress plugin of same name) not showing

I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta

How to Separate Wordpress Query Result by Taxonomy with Heading?

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

Resources