Unable to get previous_post_link to work - wordpress

I am trying to fetch previous and next custom post type link from same taxonomy within single-custom-post-type file
As per codex passing 5th parameter i.e. taxonomy name http://codex.wordpress.org/Function_Reference/previous_post_link should make it work
Here is my code
previous_post_link('%link', 'Previous Case', TRUE,'',$taxonomy)
previous_post_link('%link', 'Previous Case', TRUE,'','custom_categories')
I have tried both way i.e. passing dynamic variable as well as string, yet I cannot get it to work, What it returns is random custom post type links rather than from same category
the code is bit long so I have pasted it over pastebin http://pastebin.com/X5pKuifA

The above code works perfectly as mentioned in codex, The issue turns out to be I have allocated 2 categories mistakenly to all of the custom post type thus the pagination within same taxonomy was not working.
So If you land on this question make sure you donot have more than 1 category assigned to your posts

Related

How can I exploit the wordpress template hierarchy to render a different post template depending on category name/slug?

Apologies if this appears simple to some, but I have scaled high and low and I'm not finding a solution here to my problem, which is:
I have a website set up with Wordpress in which posts can fall under one of three categories: reviews, views, news - the slugs associated with each of these category names are the same.
Currently, calling up the web page of any individual post classified under any of these categories will see it rendered by the file single.php.
However, I want to make a slight adaption to the rendering of the post when it falls within the 'reviews' category. I have copied and renamed the original single.php file to single-post-reviews.php (no custom posts here, I will just confirm and I would like, if possible, to avoid child-theming here - not good practice, I know), but I am not seeing the new rendering from my new file.
I've also tried renaming to single-reviews.php which hasn't worked either - so could someone tell me what exactly I'm missing here?
Thanks,
WordPress Template Hierarchy for Single Posts doesn't factor in the current post category (likely due to the fact you can have multiple categories). Due to this, you have 2 viable options to your problem.
1) You can modify single.php to check for the post category, and if it's categorized under reviews, do something. This makes sense if you're just adding a small amount of markup in one or two places, or even hiding a few lines conditionally.
2) You can override the page template that's loaded based on the post's category using the single_template filter. Because I don't know exactly what you're doing, I'm going to elaborate more on this method. Take the following function:
add_filter( 'single_template', 'so51913799_review_template' );
function so51913799_review_template( $single_template ){
global $post;
if( has_category( 'reviews' ) ){
$single_template = get_stylesheet_directory() . '/single-post-reviews.php';
}
return $single_template;
}
If you put it in your functions.php file, it will make use of the has_category() function (I prefer this to in_category() since in_category just returns has_category anyways) and if it matches, it will update the $single_template variable to single-post-reviews.php. This assumes that the file is inside your /wp-content/themes/ACTIVE-THEME/ directory.

How to check that the a page uses a taxonomy in WordPress?

I need to edit a template's header.php file and I want to check if it has a taxonomy named "store". If so, then I need to extract it's tag_ID.
When I edit the page (it's not a WordPress Page type) I can see these values in the admin url:
taxonomy=store&tag_ID=720
So I know there is a way but I'm having trouble getting any good results. I tried the
method detailed here to extract at least the tag_ID but I'm getting NULL:
$tag_id=get_query_var('tag_ID');
echo $tag_id; //NULL
Edit:
To be clear in regards to the tag_ID because it may be confusing, all I really want is to get the unique id of the requested page so I figure seeing first if it has a taxonomy named "store" and then getting the right one using the tag_id.
AS far as i understand your question, You should try this:-
$data = get_queried_object();
With this, you can get what page/post/taxonomy is being called.
Just print-out/var_dump this $data variable, you will get full object of page/post/taxonomy.
Hope this may help you.

get_categories returning an empty array when it shouldn't

I'm writing a file management plugin that uses the category system to create a mock filesystem hierarchy and I'm running into some issues with some code that is responsible for recursively deleting the 'folders' (actually categories). Instead of the code returning all of the child categories of the category to be deleted, it returns an empty array. Any ideas why?
//get all child categories
$wk_child_categories = get_categories(array('child_of' => $_POST['wk_ID'], 'orderby' => 'term_group'));
echo " Got the child categories. They are:";
print_r($wk_child_categories);
One thing that I can see that might be causing the issue is the lack of the post parameter. By default if you don't have a post parameter it will fallback to the standard Wordpress "post" type. If you are using a custom post type it would return an empty array because the query is looking in the wrong spot.
There is also the possibility that the categories are empty. In the arguments for get_categories() there is a hide_empty argument that will default to not return empty categories, try setting that to 0

Custom post types permalink with parent custom post type

Hard to define the Title of this Question....
I want to create a nice readable permalink structure for my 2 custom post types (CPT).
My first CPT "family" has the following rewrite-slug "family/%postname%" (all works fine)
The second CPT "childs" has a metabox where I can select the parent_id-field by choosing a CPT "family" where the child-CPT belongs to. That also works great.
Now I set the rewrite-slug for "childs" to "%parent_post_url%/child/%postname%" so that I can get the following URL "family/the-griffons/child/peter" . But when I call this URL wordpress displays a not-found-page. The crazy thing is that if I set the rewrite-slug hard to "family/the-griffons/child/%postname%" I can call the URL (both URLs are the same!!!)
So why toes WP throws an error when I try to get the URL dynamically but not when I hardcode the URL??
The child-parent relationship you think you have is not quite there. Based on what you've told us so far, it seems that all you have is a custom field denoting the "pseudo-parent" id. So what your question should really read is
How do I rewrite the first part of the cpt url, based on the cpt custom field value ?
because, as far as wordpress is concerned in your case, that's all that "parent id" really is- a custom field value.
you can try following the last part(Part 3.) of this tutorial, keeping in mind, that you'll want the actual path of the url of the "parent id" and not the cf "parent id" value itself, you'll have to implement something along the lines of:
$parent_id = get_post_meta($post_id, "your_cf_key_for_parent_id", true);
$full_parent_post_url = parse_url( get_permalink( $parent_id ) );
$parent_post_url = $full_parent_post_url['path'];
if(!$parent_post_url) { $parent_post_url = "default-fallback" }
$permalink = str_replace(‘%parent_post_url%’, $parent_post_url, $permalink);
return $permalink;
another relevant stackexchange answer:
using-custom-fields-in-custom-post-type-url
BUT as #c0ns0l3 mentioned using custom taxonomies IS the proper way to go about this.

Best way to layer Custom Posts regarding structure and data

I would like to know the best method for layering posts in wordpress 3.0 when you need to swap back and forth between structure and data. For example, custom post type 1 has 6 custom fields, each field contains a value for the tab shortcode; i.e. [tab:data] [tab:credits] [tab:where to buy] etc - this type is standard every time the page is called and always the same post; then it needs to switch to another custom post type and select the data post it's going to use based on what tag page is being called. Then it needs to pump a custom field from data post into the same display space as the base structure post, and then hop back and forth between structure and data, and THEN run a loop. Thanks in advance.
I would just query for the two special posts before you do the loop, with get_posts (which will not mess up the regular query). Then you can display them however you want, with their information mixed together, and then do the loop.
In this example, $fixed_post is a fixed post with slug "fixed_post_slug" of the custom post type "fixed_posts". $tag_post is a post where the name equals the current tag (if we are showing a tag page), and of custom post type "tag_posts".
$fixed_post = get_posts(array('name' => 'fixed_post_slug', 'type' => 'fixed_posts'));
if ($fixed_post) {
$fixed_post = $fixed_post[0]
}
$tag_post = get_posts(array('name' => get_query_var('tag'), 'type' => 'tag_posts'));
if ($tag_post) {
$tag_post = $tag_post[0];
}

Resources