wordpress custom archive pages - wordpress

I'm developing a wordpress page and I'm looking for the best practices to send custom DB queries. I created a archive page with a list of artists. These names are stored in a custom post type's custom taxonomy. Now I want to code links on every name, which should lead the visitor to a page where all the posts that have this artists name in this custom field.
I know how to create the custom DB query, but how do I submit the name? Just over a normal POST-request? Is there a convenient way to do this within WP?
Thanks for your help.
Dan

you said in your question "custom post type's custom taxonomy"
you can create a custom taxonomy template to list all posts under a particular taxonomy. you can use this detailed tutorial how to use taxonomy.
http://code.tutsplus.com/tutorials/introducing-wordpress-3-custom-taxonomies--net-11658

I think the best way to do this would be store the artists name as a meta value attached to the post. Then you could use Wordpress inbuilt meta query to easily output all posts with the artists name stored as a meta value.
$meta_query_args = array(
array(
'key' => 'artist',
'value' => 'John Doe',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );

Related

Custom Post Types with Parent Pages - best practice

I am working on a site with product categories and individual products organized like this:
product category 1
product
product
product
product category 2
product
etc...
It was suggested to me that the best way to do organize the individual products was to create a custom post type for products. The product categories are currently pages.
So now I am trying to figure out the best way to connect a product custom post type with its particular product category page (so that url structure can be "/product_category/product/").
I am sure there is a smart and efficient way to do this, but I'm not seeing it. I was thinking that I could add categories to the pages and CPT's - but that isn't quite the effect that I want because I don't want them to only be accessible as archive pages. Is that actually a problem?
Sorry that this is such a broad question - I can get it to work but am sure that my first try would not be the best way, so I am just looking for some pointers or examples from people who have done this before to steer me in the right direction.
Thanks in advance, any help appreciated.
You can use Custom Taxonomies.
function product_categories_init() {
register_taxonomy(
'product-category',
'product', // Or 'post', whatever the custom post type is
array(
'label' => __( 'Product Categories' ),
'rewrite' => array( 'slug' => 'product-categories' ),
'hierarchical' => false,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
)
);
}
add_action( 'init', 'product_categories_init' );
This is a simple example. You'll have to check out the options in the reference link. But it is essentially your own kind of category for a post type (or an array of post types).
In the end I did get this right. Part of my problem was due to a misunderstanding about how to use categories in wordpress.
What I ended up doing was use the custom taxonomy as shown by Austin Winstanely's answer, and then refactor my page organization a little bit so that the product category was not a page post type, but a category - displayed using the category template. This makes it very convenient, because individual products in the custom post type can be associated with their product category very easily. When I asked the question I had not understood that categories could have their own template and function as web pages.
Thanks for the help!
-Alyssa

Show custom fields with query_posts

I used the plugin "Types" for wordpress.
I created a custom post called "slides" and created 2 custom fields "slide" and "phrase".
When I do :
$args=array(
'post_type' => 'slides',
'posts_per_page' => 5
);
$my_query = query_posts($args);
print_r($my_query);
I get the posts but I don't get any of the custom fields. Is there something I'm doing wrong? Thanks
Custom fields aren't returned as post objects. That is why you don't see them when doing a var_dump() of your query. You have to retrieve them manually unfortunately. You have to make use of get_post_meta to retrieve custom field data for a post
A point of note, never use query_posts. You should rather use WP_Query
WHY SHOULD query_posts NOT BE USED
Further info which you should read - query_posts() should be avoided?

Custom WP_Query not working (listing featured articles + popular articles)

I am Wordpress beginer and I want to set multiple queries on a page that lists my posts.
Featured posts
I have front page that lists featured posts from all categories,
Categories pages that lists featured posts from current category
Q: Is it better to use My featured posts new category name or to set sticky post (under public - publish options) for posts I want to feature on front page and on categories pages? Every post already have his own category like News so the My featured posts will be the second category.
Query
Let's suppose I'm using category name for featured posts (but then i got permalink to My featured posts category (site/my-featured-posts/2013/07.....) which I don't want (so maybe sticky posts are better solution)
I'm trying to setup query to list featured posts but only from standard post type, not gallery or video
<?php
$arg = array(
'cattegory_name' => 'my-featured-category',
'posts_per_page' => 5,
'nopaging' => true,
'post_status' => 'publish',
'post_type' => 'post'
);
$featured= new WP_Query($arg);
if ($featured->have_posts()):
while ($featured->have_posts()) :
$featured->the_post();
?>
and then below the_title(); ..... and so on
What I get is all articles from all categories.
Q: Also, how to I get popular articles based on views and number of comments for the last day?
Q: How to list posts which have post format video?
Q: Are there any online tools that build wp_query based on criteria?
Thank you.
You can also set posts as "featured" using a tag. You can then use tag=featured with WP_Query to get posts tagged with a certain tag ("featured" in this example).
As for your additional questions...
Q1: You can save page views to a flat file or to the WordPress database. Once I used the database to store a UNIX-timestamp as traditional WP post meta for each post page view. As it was a timestamp, I could calculate the age of the page view easily (and I could also run SQL queries to remove post meta that was older than a certain timespan).
Comments can be queried by time ordering too. See WP_Comment_Query.
Of course there are plugins that can do this for you, but for me they included useless bloat I did not need.
Q2: For querying post formats, use the query variable called tax_query as GATEKeeper did in a WP support question: http://wordpress.org/support/topic/post-formats#post-2034414.
Q3: A google query returned a thing called The WordPress Query Generator.

Add Dynamic Multiple Boxes Wordpress

I've created a custom post type in wordpress called Product, and created a custom box called "What's Included". How would I create an "Add New Included Item", to create dynamic boxes?
It would look similar to how you can add multiple custom fields, but instead of one field, it's a "What's Included" object.
Any ideas?
'What’s Included' => array(
array( '_wi_title', 'Title'),
array( '_wi_thumbnail', 'Thumbnail'),
array( '_wi_title', 'Description', 'textarea')
),
Have you considered using a custom taxonomy? Sounds like it would fit your needs perfectly, although it is indeed possible to create a dynamic form in a custom meta box. A non-hierarchal taxonomy would allow you to repeat items you include often, as well.

In WordPress how do you register built-in taxonomies with custom post types in code?

The WordPress codex has a lot of examples of how to register custom taxonomies with custom post types, but I couldn't find much about using built-in taxonomies (tags & categories) with cpts.
I have a cpt called listings, and I need to add the standard category and tag UI elements to the listing cpt page. I also need to do this with code in my functions.php, rather than using a plugin.
Not a problem at all. When you register the post type, just add this argument to the array:
'taxonomies' => array( 'category', 'post_tag' )
Suppose you defined your cpt (custom post type) by the following:
register_post_type('listings', $args); // where $args is an array of your cpt settings
Then you could use the following to add taxonomy:
// category-like:
register_taxonomy('listing_category', array('listings'), array('hierarchical' => true, ...));
// tag-like:
register_taxonomy('listing_tag', array('listings'), array('hierarchical' => false, ...);
In fact, I personally put those custom type definitions in my own plugin (not open for public as it provide my own site functionalities, which obviously not suit the others at all).
The problem of putting in functions.php increases the difficulty to change to a new theme (although changing theme is not so often, but for self-owned blog, it do happen in some day).
Moreover, the custom post types should be site-wide, not depending on the current theme. So semantically it should not be in the theme's directory.

Resources