Ask your WordPress questions! Pay money and get answers fast! (more info)

Permalink structure for query under a custom post type

  • SOLVED

I have multiple custom post types and I want to order each post type in multiple ways using custom queries. I would like each way that I order those posts to have its own permalink structure.

<strong>For example:</strong>

I have a custom post type called Media

*edit wrong post type name before edit*
I want to sort *media* a few ways: Random, Loved and Latest

What I would like the permalink structure to be for each way I sort would be:

examplesite.com/media/latest
examplesite.com/media/loved
examplesite.com/media/random


I have all of the queries ready and working, but I figured I would just have to create a page template for each query and I could just define the permalink structure when adding a new page in wp-admin. I'm pretty sure I was wrong because I when I add a "/" it gets rid of it like so:

<strong>What I enter:</strong> media/latest

<strong>What it turns into:</strong> medialatest

I checked the template hierarchy to see if I could create addition archive pages for each query, but I don't think that's the way to go about it.

Do I have to create a template for each query in my child theme and do some sort of url rewrite for each of those templates?

How do I go about doing this? Any code snippets, or tutorials?

Thank you for your help.

<em><strong>Update</strong></em>

I could create a custom taxonomy (I think?) and use the queries I built in taxonomy-latest.php , taxonomy-loved.php and taxonomy-random.php, but for some reason this is confusing if it would work or not for me. I have other categories for example sake: (cat-a, cat-b, cat-c)

What I was hoping to do is output

example.com/media/category/cat-a
example.com/media/latest/category/cat-a
example.com/media/loved/category/cat-a
example.com/media/random/category/cat-a

I'm very confused. For some reason I have it in my head that I won't be able to perform this if I use the create a custom taxonomy method. Am I right? So can I still use a taxonomy, or will I probably have to do a rewrite?

<em><strong>Update 2</strong></em>

I think I found a good tutorial on [[LINK href="http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/"]]Advanced Taxonomy Queries[[/LINK]] Can anyone tell me if I'm on the right track? Also I am wondering if latest,loved and random would be non-hierarchical taxonomies?

Answers (4)

2015-01-10

PHPSmashCode answers:

Hello,

May be these threads help

[[LINK href="http://wordpress.stackexchange.com/questions/51444/add-extra-parameters-after-permalink"]]http://wordpress.stackexchange.com/questions/51444/add-extra-parameters-after-permalink[[/LINK]]
[[LINK href="http://wpquestions.com/question/show/id/3014"]]http://wpquestions.com/question/show/id/3014[[/LINK]]

Thanks.


Chris comments:

Thank you these did help somewhat so far


PHPSmashCode comments:

Hi,

<blockquote>I have all of the queries ready and working</blockquote>

Currently how you managing Random, Loved and Latest? how you set it in dashboard?


Chris comments:

<blockquote>Currently how you managing Random, Loved and Latest? how you set it in dashboard?</blockquote>
I have all of the queries written and ready to go into what ever templates that they need too. Before I tried to use page templates and create a page with the permalinks I wanted in wp-admin.


After reading the [[LINK href="http://blog.pmg.co/a-mostly-complete-guide-to-the-wordpress-rewrite-api/"]]A (MOSTLY) COMPLETE GUIDE TO THE WORDPRESS REWRITE API[[/LINK]], in the stackechange article you posted I have a little bit more of an idea of what I may need to do.

Have you checked out the updates I made?

Thanks a lot for your time so far.


PHPSmashCode comments:

i mean, are you keeping a post meta to store 'Loved'? and are using wp_query argument orderby 'rand' and 'date' for order based on Random and Latest?

I think taxonomy thing is your new update it wasn't there yesterday.


Chris comments:

<blockquote>i mean, are you keeping a post meta to store 'Loved'? and are using wp_query argument orderby 'rand' and 'date' for order based on Random and Latest?</blockquote>

I'm using Wp_Query and its $args for the most part. "Loved" would be stored in postmeta as I remember (I'm not near my computer).

<blockquote>I think taxonomy thing is your new update it wasn't there yesterday.</blockquote>

Yeah, sorry it is new. Do you guys get alerts when I make edits to the question, or should I comment on posts to let you guys know?

Thanks


PHPSmashCode comments:

Okay. I think you don't require a taxonomy. Let me try on my localhost. Will update you.

Yes, We are getting alert when you update.

Thanks.


Chris comments:

Thanks


PHPSmashCode comments:

Hi,

I tried to rewrite url like you required, I don't think you can rewrite URL like(may be some other developers in wpquestion have solution, i don't think it is possible)

examplesite.com/media/latest
examplesite.com/media/loved
examplesite.com/media/random

because "latest", "loved" and "random" will consider as post slug from the URL.

you could do it as taxonomy, actually the taxonomy are really not going to use for posts. It is only for displaying purpose. follow the steps
1. create a taxonomy for CPT 'media'.
2. create 3 terms "latest", "loved" and "random".
3. you can create template file like
taxonomy-{taxonomy}-{term}.php
example : if your taxonomy is media_tax, file name would be

taxonomy-media_tax-latest.php
taxonomy-media_tax-loved.php
taxonomy-media_tax-random.php

you can write working code in corresponding template files.

This is a solution for the issue, may be the other developers can suggest better solution. am not sure.

Let me know if you need any help in this.

Thanks,
Hariprasad


Chris comments:

Hi Hariprasad,

Thank you for the help so far.

I created a demo custom post type, taxonomy and then created a demo category called latest. I have two problems that I'm trying to figure out.

<strong>Here's the code:</strong>
// register Custom Post Types
function create_post_types() {

register_post_type('projects', array(
'labels' => array(
'name' => 'Projects',
'all_items' => 'All Projects'
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'projects'),
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
'exclude_from_search' => false
));

}
add_action('init', 'create_post_types');


// register Taxonomies
function create_taxonomies() {

register_taxonomy('project-categories', array('projects'), array(
'labels' => array(
'name' => 'Project Categories'
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'rewrite' => array(
'slug' => 'projects-cat'
)
));

}
add_action('init', 'create_taxonomies');



<strong>1.</strong> I thought the taxonomy permalink would have the post type before it like so:

example.com/projects/projects-cat/latest

but it's like this:

example.com/projects-cat/latest

How would I use the example.com/projects/projects-cat/latest without getting a 404 or file not found page.

<strong>2.</strong>I found this arg that I guess it suppose to get rid of the "project-cat" in the permalink 'rewrite' => array('slug' => 'Newsletters', 'with_front' => false),

Codex
<blockquote>Should the permastruct be prepended with the front base. (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/). Defaults to true</blockquote>

For some reason when I use that it's not rendering the desired effect "example.com/latest". Any ideas?


PHPSmashCode comments:

Hi,

Am not clear about the second point.
May be this is what you are looking for?
[[LINK href="http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/"]]http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/[[/LINK]]

Let me know if you need my help.

Thanks.


Chris comments:

Sorry,

I'm basically trying to use this URL:

example.com/projects/projects-cat/latest/

But I get a 404 page. I can use these two

example.com/projects-cat/latest/

and

example.com/projects

But not the first URL stated

For the second question I want to figure out how to strip /projects-cat/ from the URL and just get /latest/ like this:

example.com/projects/latest/

I figured the arg 'rewrite' => array('slug' => 'projects-cat', 'with_front' => false), in register_taxonomy, but it's not.

thanks again


Chris comments:

I figured the following args in register_taxonomy, would work to get rid of projects-cat, but it's not working


'rewrite' => array('slug' => 'projects-cat', 'with_front' => false),


PHPSmashCode comments:

Hi,

Try this code


// register Custom Post Types
add_action('init', 'create_post_types');

function create_post_types() {

register_taxonomy(
'project-categories',
'projects',
array(
'label' => 'Types',
'singular_label' => 'Typ',
'hierarchical' => true,
'query_var' => true,
'rewrite' => array('slug' => 'projects'),
)
);

$args = array(
'labels' => array('name' => 'Projects','all_items' => 'All Projects'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail', 'excerpt'),
'rewrite' => array(
//'slug' => 'event',
'slug' => 'projects/%project-categories%',
'with_front' => false
),
'has_archive' => 'projects'
);

register_post_type( 'projects' , $args );
flush_rewrite_rules();
}

add_filter('post_type_link', 'projects_permalink_structure', 10, 4);
function projects_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%project-categories%' ) ) {
$project_categories_term = get_the_terms( $post->ID, 'project-categories' );
$post_link = str_replace( '%project-categories%', array_pop( $project_categories_term )->slug, $post_link );
}
return $post_link;
}

Hope it solves the issue.

Thanks.


Chris comments:

The good news is that this works:

www.example.com/projects/latest

The bad new is that this doesn't:

www.example.com/projects

Once the rewrite rules have been flushed I can get rid of flush_rewrite_rules(); right?

Thanks for the help so far!


Chris comments:

Just found a [[LINK href="https://github.com/davedg629/WP-No-Taxonomy-Base/blob/master/wp-no-taxonomy-base.php"]]github plugin[[/LINK]] that removes selected category bases!

But it is working kind of funny. I'm working on a multisite setup and my main site url is setup like localhosts/wordpress/blogs/%posttype%
and
localhosts/wordpress/blogs/%category% It only works if I visit

localhost/wordpress/latest
and
localhost/wordpress/blogs/project-cats/latest (normal functioning url)


Chris comments:

Just found another [[LINK href="https://wordpress.org/plugins/remove-taxonomy-base-slug/"]]plugin[[/LINK]] that seems to be working! I'm using the original code I put for the CPT and Taxonomy. I just need to figure out how you got projects in front of the category in the url

example.com/projects/latest

what I can currently use:

example.com/projects

example.com/latest

Thank you for your help!


Chris comments:

I'm pretty sure I just got the permalinks working the way I need them. Now I just gotta test putting the different query into the template.


Chris comments:

Wait nope now there's a conflict with example.com/projects


PHPSmashCode comments:

Hello,

The last code i sent to you is working on my localhost(Not multisite) for the following links

http://localhost/lab/projects/
http://localhost/lab/projects/latest/

lab is my wordpress root. Permalink structure is just /%postname%/.


Chris comments:

Okay, your code is working along side the plugin I downloaded. Something is still kind of strange, but I really don't mind it. I'm using subdirectories on multisite and doing the testing of these functions on my main site. On my main site the url is prefixed with /blog/ for example:

localhost/wordpress/blog/%posttype%/
localhost/wordpress/blog/%posttype%/%postname%/

To get the index, or single page for projects after your code, the url above won't work, but these will:

http://localhost/wordpress/blog/projects/latest/
http://localhost/wordpress/projects/latest/
http://localhost/wordpress/projects/latest/a-single-latest-project-test/

I just have to think about if there would be any consequence to that and then work on the archive queries.

If I have problems with the taxonomy template custom queries I'll raise the prize. Thank you for your incredible help so far!



Chris comments:

Just noticed that I'm getting this warning with the code you gave me:

Warning: array_pop() expects parameter 1 to be array, boolean given in C:\xampp\htdocs\wordpress\wp-content\themes\mytheme\functions.php on line 573


Chris comments:

I also just noticed that

the permalinks are getting messed up on localhost/wordpress/projects

They look like this

<a href="&lt;br /&gt;
&lt;b&gt;Warning&lt;/b&gt;: array_pop() expects parameter 1 to be array, boolean given in &lt;b&gt;C:\xampp\htdocs\wordpress\wp-content\themes\mytheme\functions.php&lt;/b&gt; on line &lt;b&gt;573&lt;/b&gt;&lt;br /&gt;
http://localhost/wordpress/projects//practice-category-for-projects-cpt/">Practice category for projects CPT</a>


Chris comments:

Just another update:

I just figured out how to order posts how I want in taxonomy-project-categories-latest.php using the pre_get_posts action.


PHPSmashCode comments:

Here is the final working code,


//This is a test posttype

add_action('init', 'create_test_post_types');

function create_test_post_types() {
$args = array(
'labels' => array('name' => 'awesome','all_items' => 'All awesome'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail', 'excerpt'),
'rewrite' => array(
//'slug' => 'event',
'slug' => 'awesome'),
'has_archive' => true
);

register_post_type( 'awesome' , $args );
}
// add a new query variable
add_filter('init', 'awesome_pt_archive_qv');
function awesome_pt_archive_qv() {
global $wp;
$wp->add_query_var('latest');
}

// add a new rewrite rule
add_filter('init', 'awesome_archive_rewrite_rule');
function awesome_archive_rewrite_rule() {
// we're adding »latest« as a possibility to the slug
// of the custom post type archive we registered above
// no need to match anything, e.g. with »$matches[1]«
// so we're defaulting the latest query variable to 1
// this kind of emulates a endpoint
add_rewrite_rule( "awesome/latest$", "index.php?post_type=awesome" . '&latest=1', 'top' );
}

// load the »latest« template
add_filter( 'template_include', 'awesome_latest_template_redirect' );
function awesome_latest_template_redirect( $original_template ) {
// we're loading the template conditionally,
// but only if we're actually at the »latest« "endpoint"
if ( 1 == intval(get_query_var('latest')) ) {
// you've to create the template you want to use here
return get_template_directory().'/archive-latest.php';
} else {
return $original_template;
}
}

2015-01-10

Eff Jay answers:

Hi,

You can make different your media posts by adding different categories to the media posts accordingly.

Example: http://screencast.com/t/Ty4vDF00

After that you can add those categories to the main menu to list posts related to the categories exactly.

Helping Screenshot: http://screencast.com/t/2ZSrK4Hm

Regards,

Eff Jay


Chris comments:

Sorry Eff Jay, I just saw that you commented. Thank you for your help so far!

2015-01-10

Bob answers:

You can create one main page media and 3 sub pages latest, loved and random. Then create 3 page tkplates for each and assign to sub pages.

if you want to use only single template and fetch value from parmalink then you need to use rewrite rules.


Bob comments:

You can also create custom texonomy for your post type. Then create that texonomy template.


Chris comments:

<blockquote>You can also create custom texonomy for your post type. Then create that texonomy template.</blockquote>

I think I'll have to go with a combination of a rewrite/creating a custom taxonomy

2015-01-10

Sai kumar answers:

Hi,

If you want to change the permalink structure manually, then it fine with some plugins called enhanced custom permalinks. https://wordpress.org/plugins/enhanced-custom-permalinks/
Using that plugin u can edit the permalink structures to custom structure. Please check and update me.

Thanks


Chris comments:

Hi Sai,

<blockquote>If you want to change the permalink structure manually, then it fine with some plugins called enhanced custom permalinks. https://wordpress.org/plugins/enhanced-custom-permalinks/
Using that plugin u can edit the permalink structures to custom structure. Please check and update me.
</blockquote>

Do you think I'll be able to use this plugin instead of the bulk of the linked tutorial I put in update 2?


Sai kumar comments:

Hi,

I used that plugin for custom url structure. I can able to edit the url to specified structure. You please try once and if it is possible you can use that. But please note, it is not automatic plugin to rewrite the permalink, we need to edit the url manually.

Thanks
Sai


Chris comments:

Hi Sai,

Do you think this plugin would help get rid of the category front like I asked in the question to Hariprasad?
<blockquote>
2.I found this arg that I guess it suppose to get rid of the "project-cat" in the permalink
'rewrite' => array('slug' => 'Newsletters', 'with_front' => false),


Codex
Should the permastruct be prepended with the front base. (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/). Defaults to true


For some reason when I use that it's not rendering the desired effect "example.com/latest". Any ideas?</blockquote>