Wordpress Post Sorting? - wordpress

I to allow my users to sort the list of wordpress posts by Title, Date added, Comments, Rating etc. These will all be buttons or links at the top of the list (kinda like table view sorting)
However, I have no PHP experience and only know a few basics. How would I achieve this? I currently use the following line to sort my posts by name ASC:
<?php query_posts( $query_string . "&orderby=title&order=ASC" ); ?>
I was personally thinking of the following solution: orderby=VARIABLE1&order=VARIABLE2
Where VARIABLE1 would be set to title by default and VARIABLE2 would be set ASC by default. However I don't know if this is the best solution, and I know even less on how to achieve this.
Thanks for any future help!

Swen, attach ?orderby=title or ?orderby=date to the URLs of your buttons, WordPress will do the ordering itself.

One of the easiest way is just to use the Post Types Order plugin

Related

Preventing search engines from indexing all posts

I'm working on a Wordpress site where I'm using the posts to create a list of tour dates for an entertainer. With ACF I have fields set up in a table and the client just enters a date, location, link to buy tickets, etc.
The table is all I need visitors to see. The actual post created by single.php is not going to be styled and should never be seen.
I want to prevent someone searching the artist and city and coming across the post.
Is there a plugin or a disallow I can put in the robot.txt file?
Any help is appreciated. Kinda funny in a time where everyone is trying to get noticed by search engines and I want to hide something from them!
Add the code below to your themes functions.php:
add_action('wp_head', 'your_prefix_noindex_nofollow');
function your_prefix_noindex_nofollow() {
if(is_single()){
echo '<meta name="robots" content="noindex,nofollow"/>';
}
}
You can also change "your_prefix" in the function name to whatever you like. It will work as is, but it's a good practice to use the same prefix in all your function names.

Page Template for every custom post type

I'm just new in wordpress, I just wanted to ask if it's a bad practice if I'll create page template for every custom post types?
I mean if I have CPT [custom post type] with different content, like my first CPT has images, my 2nd cpt has image and text, my last CPT, has slider, text and description.
Because I wanted to create page template for every type of CPT's I have. Is it a bad practice? Or are there efficient and effective ways to do such things?
Your answers are highly appreciated. Thanks!
It's better not to do that, not only because it's a server load, but also because you'll get crazy if you have an issue. Instead, use conditionals in the same page. You can read about it at WP Codex: Conditional Tags : Taxonomies and then pay attention to the is_tax and has_term tags.
This way, you can use is_tag or has_term depending on your approach, like this
if( has_term( 'jazz', 'genre' ) ) {
// do something
}
You can use taxonomy to deal with such problem. Use taxonomy to filter contents according to your requirements. Use register_taxonomy() to register a taxonomy for CPT.

Wordpress query_posts to modify wp_query

When I alter main $wp_query with query_posts function, all my conditional tags are false and all pages have [is_home] => 1.
So all custom templates I made by modifying main query to save some time are now home. Anyone knows a fix for this?
You should never use query_posts to create custom queries. query_posts breaks the main query, as you have seen. Whether or not to use a custom query for your specific needs, I don't know.
I have done a complete post about this subject on WPSE that you can go and check here. Also go and check out Theme Development in the codex

Assigning two routes to the same category in Wordpress

I wonder if it is possible to assign two routes to the same category. For example:
By accessing http://myblog.com/action or http://myblog.com/movie-action, leads me to the same category destination.
Thank you
i Think this plugin can handle this
http://wordpress.org/plugins/quick-pagepost-redirect-plugin/
it makes all kind of redirections in wordpress
One way to do this is to make all posts have both categories and then the links come up the same.
However, to do literally what you are asking, let's say you want to get to Category Action through Category Movie Action. See making categories in the Codex.
Create a .php file for the category you want to redirect. In this, make
category-movie-action.php
In this file, you can just paste the code
<?php
header( 'Location: http://myblog.com/action' ) ;
?>
And that should redirect anyone who goes to that particular category. If you need a more elegant solution I'm sure there are ways as well.

Only date certain articles on WordPress?

I'm setting up a WordPress installation where I want some of the articles to show the date they were posted, and other articles to leave the date out. I'd like these articles to be completely dateless, if possible, so they only show in category archives and not in date archives.
I'm guessing I can tweak the templates to show the date or not based on the article's category, I was wondering if there was an easier solution to this?
Or should I start writing my own plugin to do this?
I've not got anything online at the moment, this is just an idea I'm churning over in my head for now.
Cheers,
Dan
Your theory of how to do it (have the theme files make a check for the category, then either display the date or not) is correct.
I think this code should do it:
<?php
if (is_category('CategoryThatDisplaysDates')) {
echo '<p>Date posted: '; the_date(); echo '</p>';
};
?>
If you don't want to mess up your categories (having “CategoryThatDisplaysDates” in a category listing looks a bit weird), you could try custom fields (meta-data). You add a custom field, e.g. display-date, in the write post panel and set its content to true.
Then, use ahockley's code, just change if(is_category(...)) to
if(get_post_meta($post->ID, 'display-date', true) == 'true')
i'd go with a custom field. you can read about using custom fields here: http://codex.wordpress.org/Using_Custom_Fields

Resources