Query custom taxonomy wordpress error - php

I have a custom query which I'm trying to run. How I want it to work is,
Firstly, check the page title
if the page title is the same as the name of the cat from a custom taxonomy then show all the posts that are in that custom taxonomy - post type.
The only trouble is, it isn't returning anything, I've made sure I'm targeting the correct 'post type' & correct 'taxonomy' name. It will return the cat id with the following:
$cat->cat_ID;
But won't return any posts, here is my code:
<?php
// Get the name of the page
$theTitle = get_the_title();
//Get the taxonomy for the custom post type
$categoryselect = array( 'taxonomy' => 'team-members' );
$categories = get_categories($categoryselect);
// loop through each category as cat
foreach ($categories as $cat):
//If cat is the same as title *name* then lets do something
if($theTitle == $cat->cat_name):?>
<h3>We’re here to help.</h3>
<?php
$catID = $cat->cat_ID;
//echo $catID;
//query the posts but, use the cat ID so the page relates to it.
$args = query_posts(array( 'post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1 ));
$loop = new WP_Query( $args );
// run the loop for posts
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="person">
<h5><?php the_title(); ?></h5>
</div>
<?php endwhile;
endif;
endforeach;
This is on a page.php template
Any suggestions?

You have a couple of issues here. Lets start with this line
$args = query_posts(array( 'post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1 ));
query_posts should never be used, and nor should you make use of two queries in one
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Secondly showposts has been depreciated in favor of posts_per_page
You terminology is incorrect, and therefore you are making use of wrong parameters in your query. You are not working with categories here, but with a custom taxonomy and terms. To get a good perspective of categories, terms and custom taxonomies, see this post I have done on WPSE
You should make use of a tax_query and not the category parameters in WP_Query
To come back to how you get your terms. The way you are doing it with get_categories() is not wrong, but it can become confusing as you are actually working with a custom taxonomy and not the build-in category taxonomy. I would suggest to use get_terms() instead
I actually feel that you don't need to use get_terms, get_categories or the foreach loop. I have checked your code and it seems the only time that something will show is when the term name is equal to the page name.
You already have the taxonomy name and the term name, the only thing that you can maybe do is to check if the term exists, and then feed that to your custom query
This is a modified version of your code, UNTESTED
<?php
// Get the name of the page
$theTitle = get_the_title();
//Get the taxonomy for the custom post type
$taxonomy = 'team-members';
//Set the page title as term name
$term = $theTitle;
if( term_exists( $term, $taxonomy ) ) : // Check if there is a term that match the page title ?>
<h3>We’re here to help.</h3>
<?php
//query the posts but, use the cat ID so the page relates to it.
$args = array(
'post_type' => 'team',
'orderby' => 'title',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'name',
'terms' => $term,
'include_children' => false
),
),
);
$loop = new WP_Query( $args );
// run the loop for posts
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="person">
<h5><?php the_title(); ?></h5>
</div>
<?php endwhile;
endif;
?>
EDIT
Have now tested the code and made a few minor adjustments. It is working 100% now on my local install.

Related

WordPress - Shortcode for Custom Post Type Category

Hej, i want to Display all Posts from one Category of my Custom Post Type with a Shortcode.
Example:
My-Custom-Post-Type:
Tomatoe, Lettuce, Fruit, Vegan, Medium Rare, Rare
Food-category:
Burger, Pizza, Salad
Burger:
Vegan, Medium Rare, Rare
Salad:
Tomatoe, Lettuce, Fruit
Is there a way to do this?
Sorry for bad Example
From your example I think you have confused CPTs with Taxonomies and Terms, but in general all you have to do is first create a custom shortcode by adding this in your functions.php:
function your_custom_function ( $atts ) {
//Run your Query
//Iterate and display output
return $output;
}
add_shortcode( 'your_shortcode', 'your_custom_function' );
Then inside your function you would need a query to get and display the posts you want, example:
$args = array(
'post_type' => 'my_custom_post_type',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'cat' => 3,
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li'> . the_title() . '</li>';
endwhile;
wp_reset_postdata();
This tool can also help you with your custom query.

WordPress - Code to display post of certain category and display a custom field

I've done hours of Google searching but still stumped on where to start.
I'm trying to display a grid of posts in a certain category, whilst also grabbing and displaying a custom field in that post.
I just need a starting point, then I can figure out how to implement it and then style it.
Any help would be greatly appreciated!
I think you could try this code:
$args = array(
'post_type' => 'medlem',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 4
)
)
);
As 'field' => 'term_id' is default you can skip this line. For more information go to: http://codex.wordpress.org/Class_Reference/WP_Query
You can display a list of post in the grid by using category id in an array with a query like below:
<?php global $post;
$args = array( 'posts_per_page' => -1, 'offset'=> 1, 'category' => array(1,2,4) );
foreach ( $myposts as $post ) : setup_postdata( $post );
$custom_field = get_post_meta($post->ID, 'your_key', true); // TO get custom field value of post.. ?>
<?php the_title(); ?>
<?php endforeach;
wp_reset_postdata(); ?>
You can also get custom field value by using get_post_meta function with post id like mentioned above code.
Hope this will helpful for you. Thanks.

How can I exclude only latest post in certain category from showing in loop?

I have two loops and queries.
At the top of page I have this code. It only shows the latest post from category named "featured":
<?php
$latest_featured_post = new WP_Query ( array ( 'category_name' => 'featured', 'posts_per_page' => 1 ) );
while ($latest_featured_post->have_posts()) : $latest_featured_post->the_post();
?>
Now I want to exclude that post from the other, main, loop on the same page, because I don't want it to show twice. I tried to achieve that by catching the ID of a latest post in a "featured" category and passing it to the 'post__not_in' argument but I did something wrong. This is my code
<?php
$category_id = get_cat_ID('Događaji');
$exlude_latest_featured_post = array($latest_featured_post->ID);
$args = array(
'category__not_in' => array($category_id),
'post__not_in' => $exlude_latest_featured_post,
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php get_template_part('loop/content'); ?>
I tried to manually pass ID of the post ('post__not_in' => array(1337) for example) and it works. Which means that I made mistake with catching the "featured" latest post ID.
I was searching Google for the answer but I didn't find anything helpful. Hope someone here has time and right answer
Thanks
You can capture the featured post id withing the 1st loop via get_the_id function, then use it in the later loop:
<?php
$latest_featured_post = new WP_Query ( array ( 'category_name' => 'featured', 'posts_per_page' => 1 ) );
while ($latest_featured_post->have_posts()) :
$latest_featured_post->the_post();
$featuredID = get_the_id();
?>
Your latter loop:
$category_id = get_cat_ID('Događaji');
$exlude_latest_featured_post = array($featuredID);

How to display recent ten posts of specific category when visiting that specific category's link/url

When i visit mysite.com/category/*** It only displays last post of that category.
But i want it to display recent ten posts of that category.
In short...
I want my site http://bishwash.com.np/category/entertainment/ to display posts like http://www.onlinekhabar.com/category/bichitra-world/
you can get the recent posts with wp_recent_posts($arg,ARRAY_A );
<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
You can use get_posts to retrieve posts.
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&category=1');
foreach($myposts as $post) :
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Change category=1 to your category ID number.If you to get post from XYZ category and XYZ category had 13 ID number then it would be something like this category=13.
In categroy.php page before
<?php if ( have_posts() ) : ?>
//paste the following code
$catID = the_category_ID();
$args = array( 'numberposts' => '10','category' => $catID);
$recent_posts = wp_get_recent_posts( $args );
setup_postdata($recent_posts);
At the end of loop
wp_reset_postdata();
The number of posts is set globally for all index type pages (including the home page and archives and category) in settings, reading
It seems you have limited it to 1 ? Then it's better design to set it to 10 because that will apply to all pages, and design a specific template for the home page, where you limit the number of posts with the methods given to to.
You can achieve that in two ways :
- build a page template, with a specific query, create a page with this template, and define it as homepage in settings -> reading
- create a home.php file in your theme
In both cases, it's better to have a child theme, not to loose modifications when your theme is updated.
Create the template (sample.php) and write code like
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
or use CATEGORYNAME
Note: When we click the link it redirects to to the sample.php and the category name is dynamic that means when we click the category its automatically stored to the CATEGORYNAME

How to show only the posts from the most recent category of a custom taxonomy in WordPress

I have a custom taxonomy called Issues (like magazine issues) with categories named after the title of each issue. I created a page called "Current Issue" and added a link to it in the main navigation of the site.
This is the loop I have in the page template now:
$categories = get_terms('issue', 'orderby=count&order=asc');
foreach( $categories as $category ):
?>
<h3><?php echo $category->name; ?></h3>
<?php
$posts = get_posts(array(
'post_type' => 'issue_posts',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true,
));
foreach($posts as $post):
setup_postdata($post);
It does order the categories and posts appropriately, but this pulls in all the posts for all the categories. I need the link to show only the most recently added category.
The most recently added term will be the one with the highest ID. Fetch a single term ordered by ID in descending order and you will have the one most recently added.
$args = array(
'number' => 1,
'orderby' => 'ID',
'order' => 'DESC'
);
$recent_issue = get_terms( 'issue', $args );
Because categories are a taxonomy in Wordpress, there is no date associated with them to tell you what the "most recent" one is. If you need to keep track of what the most recently created category is, you could use the created_term, which in the Wordpress source is in /wp-includes/taxonomy.php and function wp_insert_term():
do_action("created_term", $term_id, $tt_id, $taxonomy);
The $taxonomy for a category will be category (check before saving), and the $term_id will be the your category ID. From your function hooked to this action, you can call update_option('latest_category_id', $term_id); and then fetch it later with get_option('latest_category_id'); for use in your get_posts() call.

Categories