Show Custom Post based on selected custom taxonomy in Wordpress - wordpress

In the current website i am building i build the following functionality.
Current situation to give a better understanding of the problem.
There is a page called blog. This page display's all the blogs (posts) in a list. There is a aside with all the categories the posts have. The user can select a category. Once the user clicks on it the user will go to the category.php and see all the posts that have that specific category.
I want to create the same scenario but than with a custom post type. I have a template part; 'offer-list-template.php'
offer-list-template.php (here i get all the offers and display them);
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'Offers',
'posts_per_page' => 10,
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php
//$objectData is used in post-listing-item.php
$objectData->title = get_the_title($post);
$objectData->content = get_the_content($post);
$objectData->permalink = get_the_permalink($post);
$objectData->thumbnail = get_the_post_thumbnail($post);
$objectData->posttype = get_post_type($post);
include(locate_template('template-parts/post-listing-item.php'));
?>
<?php endwhile; ?>
In the same file there is the aside which shows the categories. offer_category is the taxonomy slug.
<?php $terms = get_terms( 'offer_category' );
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<li>' . $term->name . '<span>('. $term->count . ')</span></li>';
}
?>
</ul
The result is:
If the user clicks on the categorie it goes to: taxonomy-offer-category.php (taxonomy-slug.php)
Here i need to display the posts (post_type->offers) that have the selected category.
Registration of the custom post type:
//Register Custom post type for Offers.
function create_posttype_offer() {
$args = array(
'labels' => array(
'name' => __('Offers', ''),
'singular_name' => __('Offer'),
'all_items' => __('All Offers'),
'add_new_item' => __('Add New Offer'),
'edit_item' => __('Edit Offer'),
'view_item' => __('View Offer')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'Offers'),
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'capability_type' => 'page',
'supports' => array('title', 'editor', 'thumbnail'),
'exclude_from_search' => true,
'menu_position' => 70,
'has_archive' => true,
'menu_icon' => 'dashicons-star-filled'
);
register_post_type('Offers', $args);
}
add_action( 'init', 'create_posttype_offer');
// Register Custom Categoeries for Custom Post Type Offers
function taxonomies_offer() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'offer_category', 'offers', $args );
}
add_action( 'init', 'taxonomies_offer', 0 );
When i use the default post type and i call category.php which has the following code is will display the posts with the selected category. But with a custom post type i can't find a way to manage it.
<?php if (have_posts() ) : while ( have_posts() ) : the_post(); // run the loop ?>
<?php
//$objectData is used in post-listing-item.php
$objectData->title = get_the_title($post);
$objectData->content = get_the_content($post);
$objectData->permalink = get_the_permalink($post);
$objectData->thumbnail = get_the_post_thumbnail($post);
$objectData->posttype = get_post_type($post);
include(locate_template('template-parts/post-listing-item.php'));
?>
<?php endwhile; ?>
This is the post-listing-item (view)
<article class="post-item">
<figure>
<?php echo $objectData->thumbnail ?>
</figure>
<div class="content">
<a href="<?php echo $objectData->permalink ?>">
<h2><?php echo $objectData->title ?></h2>
</a>
<p><?php echo $objectData->content ?></p>
<div class="read-more-button">
<a href="<?php echo $objectData->permalink ?>">read more
<span>
<svg class="next-arrow"><use xlink:href="#next-arrow" /></svg>
</span>
</a>
</div>
</div>
</article>

By understanding, you have to have a query for having all the custom taxonomy post to load by the following code:
$terms = get_terms(
array(
'taxonomy' => 'offer_category',
'hide_empty' => false,
)
);
foreach ($terms as $term){
$args = array(
'post_type' => 'Offers',
'tax_query' => array(
array(
'taxonomy' => 'offer_category',
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$query = new WP_Query($args);
if($query->have_posts()): while($query->have_posts()): $query->the_post();
the_title();
the_content();
endwhile;
wp_reset_postdata();
endif;
}
Hope this works for you

I found it!
I got the taxonomy and slug from the url and used that in the query.
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$the_query = new WP_Query( array(
'post_type' => 'offers',
'tax_query' => array(
array (
'taxonomy' => $taxonomyName,
'field' => 'slug',
'terms' => $term_slug,
)
),
));
?>
<ul>
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

Related

Query posts by custom fields

I am building a website using Advanced Custom Fields that has yachts for sale. The yachts use a custom Post type called ‘yachts’
One of the fields in the individual yacht listing is a true/false value to determine wether it should be a featured listing. (it was originally a checkbox but I changed it after experimenting with the answer in this post)
Advanced custom fields: can't query posts by custom field
I am trying to display previews of the featured listings on the home page but I can’t get it to work.
I originally tried this code from the ACF documentation
edit: I started with just trying to fetch the title but I also need the additional fields
<section class="featured-yachts">
<h2>FEATURED YACHTS</h2>
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'yachts',
'meta_key' => 'featured',
'meta_value' => 'yes'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</section>
And now have this after switching to a true false field
<section class="featured-yachts">
<h2>FEATURED YACHTS</h2>
<?php
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured',
'value' => '1',
'compare' => 'LIKE',
)
),
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
echo get_the_title();
endwhile;
wp_reset_postdata();
}
?>
</section>
I have also tried editing this: How filter custom posts by Advanced Custom Fields checkbox
but again, I cant get it to work.
The most any of these return is the h3 title
I can't work out what the issue is.
Thanks in advance for any help
edit : I have at least one post using the custom post type 'yachts' that is set to true for featured.
I still have lots of fields to add, but I would have expected that this output the h3 title and then the title of the post marked featured.
In the end, I would like it to function much like latest post previews, but display custom fields and only if the 'featured' true/false is set to yes
I have made an ACF field called 'featured yachts' and registered the block with Gutenberg, but there are no actual ACF fields within it, it's only used to call the file 'content-featured-yachts-block.php' which has this code I am trying to fix.
I am trying to populate the post previews within this featured yachts block with data pulled from the individual yacht listings. In these listings is the 'featured' true/false option. Screen shot atttached.
This is my code for registering the CPT
// Our custom post type function
function create_posttype() {
register_post_type( 'yachts',
// CPT Options
array(
'labels' => array(
'name' => __( 'Yacht Listings' ),
'singular_name' => __( 'Yacht' ),
'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ),
'add_new' => __( 'New Yacht Listing'),
'add_new_item' => __( 'Add New Yacht Listing'),
'edit_item' => __( 'Edit Yacht Listing'),
'new_item' => __( 'New Yacht Listing'),
'view_item' => __( 'View Listings'),
'search_items' => __( 'Search Listings'),
'not_found' => __( 'No Yacht Listings Found'),
'not_found_in_trash' => __( 'No yacht Listings found in Trash')
),
'public' => true,
// 'has_archive' => true,
'rewrite' => array('slug' => 'yachts'),
'show_in_rest' => true,
'menu_icon' => 'dashicons-sos',
'hierarchical' => true,
'taxonomies' => array('category')
)
);
}
Another edit - I made the assumption that if I could get the title field to show, the other fields would be easy but i can't get them either so I ALSO need to work out how to add the additional fields from the listing
The code I have tried is below
<h2>FEATURED YACHTS</h2>
<?php
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'LIKE',
)
),
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();?>
<h2><?php the_title(); ?></h2>
<?php
get_post_meta( get_the_ID(), 'price', true );
?>
<?php endwhile;
wp_reset_postdata();
}
?>
AND ALSO
<section class="featured-yachts">
<h2>FEATURED YACHTS</h2>
<?php
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'LIKE',
)
),
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();?>
<h2><?php the_title(); ?></h2>
<p><?php the_field( 'price' ); ?></p>
<?php endwhile;
wp_reset_postdata();
}
?>
</section>
You can use 'meta_query' and for ACF true and false you don't have to compare with LIKE. Try the below code.
<section class="featured-yachts">
<h2>FEATURED YACHTS</h2>
<?php
$args = array(
'post_type' => 'yachts',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featuredyacht',
'value' => '1',
)
),
);
$my_posts = new WP_Query($args);
if ( $my_posts->have_posts() ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post();
echo "Title - ".get_the_title()."</br>";
echo "Price - ".get_field( "price", get_the_ID() )."</br>";
echo "Length - ".get_field( "length", get_the_ID() )."</br>";
echo "Year built - ".get_field( "year_built", get_the_ID() )."</br>";
echo "Capacity - ".get_field( "capacity", get_the_ID() )."</br>";
endwhile;
wp_reset_postdata();
}
?>
</section>
To use multiple custom field parameters in your query, replace $args as in Bhautik's answer with something like this:
$args = array(
'post_type' => 'yachts',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featuredyacht',
'value' => '1',
),
array(
'key' => 'price',
'value' => 50000,
'type' => 'numeric',
'compare' => '>=',
)
),
);
The above example fetches only yachts with featuredyacht set to 1 or true AND with a price of 50,000 or more. Adjust as needed to suit your parameters.
For example, to query featured yachts within a specific price range:
$args = array(
'post_type' => 'yachts',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featuredyacht',
'value' => '1',
),
array(
'key' => 'price',
'value' => array( 50000, 100000 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
)
),
);
The developer documentation on WP_Query should be very useful to you for any further customizations.

Wordpress - Trying to retrieve only immediate children of custom post in loop

I have a custom post type called 'location' where the top level is state, second level down is city, and the third level is the actual location.
I've used the methods recommended here and here with no luck. I've tried get_pages(), get_posts(), get_children() and it's either retrieving all children or retrieving all pages.
Here is my current code that is in the single template of the custom post type:
<?php $args = array(
'child_of' => $post->ID,
'parent ' => $post->ID,
'hierarchical' => 0,
'sort_column' => 'menu_order',
'sort_order' => 'asc',
);
$locations = get_pages($args); ?>
<?php
foreach ($locations as $location):
$title = get_the_title($location->ID);
$locale = get_field('location',$location->ID);
$lat = $locale['lat'];
$lng = $locale['lng'];
$addy = $locale['address'];
$hours = get_field('hours',$location->ID);
$link = get_the_permalink($location->ID);
?>
<div class="location-single" data-lat="<?php echo $lat; ?>" data-lng="<?php echo $lng; ?>">
<?php echo $title; ?>
<address><?php echo $addy; ?></address>
<time><?php echo $hours; ?></time>
</div>
<?php endforeach; ?>
EDIT: To cover all my bases, here is the code i'm using to register this custom post type.
function custom_post_type() {
$labels = array(
'name' => _x( 'Locations', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Location', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Locations', 'text_domain' ),
'name_admin_bar' => __( 'Location', 'text_domain' ),
'archives' => __( 'Locations Archives', 'text_domain' ),
'attributes' => __( 'Location Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Location:', 'text_domain' ),
'all_items' => __( 'All Location', 'text_domain' ),
'add_new_item' => __( 'Add New Location', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Location', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'view_items' => __( 'View Items', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Location', 'text_domain' ),
'description' => __( 'Locations', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor','page-attributes' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-location',
'menu_position' => 8,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'location', $args );
}
add_action( 'init', 'custom_post_type', 0 );
I'm inserting this into the the_content area using this:
add_filter('the_content', 'location_content');
function location_content( $content ) {
if ( is_singular( 'location' ) ) {
include dirname( __FILE__ ) . '/templates/location.php';
}
else {
return $content;
}
}
Could this be the issue? Would it work better if I had a single template with this code on it instead?
EDIT: I've tried including this code and also the solution below in the single template file directly and it still returns nothing.
First, make sure you're placing this code within the main WordPress loop so you have access to $post. If you're not, you'll need to declare with global $post;.
if ($post->post_parent) {
$children = wp_list_pages(array(
'title_li' => '',
'child_of' => $post->post_parent, // child of only the parent page
'echo' => 0, // do not echo
'exclude' => $post->ID, // exclude the parent page from being added to array
'post_type' => 'location' // only posts from the location post type
));
}
if ($children) {
echo '<ul>' . $children . '</ul>';
}
You should be able to work this into your theme file and display any other info you want to outside of this. You could also do a foreach() loop on $children if you needed to in in case you need to add other content. I have this exact code running on a theme and it is working as intended. This does not get the page content, just the page titles with links. If I understand correctly, that's what you're looking for.
Change pet_pages to get_posts, then modify the arguments you pass in.
<?php
$args = array(
'post_type' => 'location',
'parent ' => $post->ID,
'sort_column' => 'menu_order',
'sort_order' => 'asc',
// etc...
);
$locations = get_posts($args);
?>
Important here is to set the post_type to your custom post-type. If you set the parent config to the current $post->ID, not necessary to also set child_of.
Alright, it's a bit of a kludge, but it works...
<?php
$post_id = $post->ID;
$level = count(get_post_ancestors( $post_id )) + 1;
switch($level) {
case 1: ?>
<?php $args = array( 'child_of' => $post_id, 'parent ' => $post_id,'hierarchical' => 0, 'sort_column' => 'menu_order', 'sort_order' => 'asc', 'post_type' => 'location',);
$mypages = get_pages( $args );
foreach( $mypages as $post ): ?>
<?php
$post_id = $post->ID;
$level = count(get_post_ancestors( $post_id )) + 1;
switch($level) {
case 1: ?>
<?php break;
case 2: ?>
<?php $location = get_field('location', $post->ID); ?>
<div class="location-single" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4><?php echo get_the_title($post->ID); ?></h4>
<address><?php echo $location['address']; ?></address>
<time><?php the_field('hours',$post->ID); ?></time>
</div>
<?php break;
}; ?>
<?php endforeach; ?>
<?php break;
case 2: ?>
<?php $args = array( 'child_of' => $post_id, 'parent ' => $post_id,'hierarchical' => 0, 'sort_column' => 'menu_order', 'sort_order' => 'asc', 'post_type' => 'location',);
$mypages = get_pages( $args );
foreach( $mypages as $post ): ?>
<?php
$post_id = $post->ID;
$level = count(get_post_ancestors( $post_id )) + 1;
switch($level) {
case 1: ?>
<?php break;
case 2: ?>
<?php break;
case 3: ?>
<?php $location = get_field('location', $post->ID); ?>
<div class="location-single" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4><?php echo get_the_title($post->ID); ?></h4>
<address><?php echo $location['address']; ?></address>
<time><?php the_field('hours',$post->ID); ?></time>
</div>
<?php break;
}; ?>
<?php endforeach; ?>
<?php break;
case 3: ?>
<?php $location = get_field('location', $post_id); ?>
<div class="location-single" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4><?php the_title(); ?></h4>
<address><?php echo $location['address']; ?></address>
<time><?php the_field('hours'); ?></time>
</div>
<?php break;
}; ?>
I'm basically looping through the loop and either printing nothing depending on the level of hierarchy.
EDIT: nevermind, it's not passing the post ID's into the nested loops.
Found the answer for my usecase:
Instead of get_pages or get_posts I'm using new WP_Query. that solved all my problems. Thank you everyone for helping.

Featured Image for Custom Post Type is not showing inside loop

I have a Custom Post Type. Each post has a Featured Image. In my index.php I am want to show up the title of each post with its Featured image. The following code shows the title but not the image.
<?php $query = new WP_Query(
array(
'post_type' => 'platform',
'status' => 'publish'
) );
if ( $query->have_posts() ) {
while ($query->have_posts()) {
$query->the_post(); ?>
<p><?php the_title(); ?></p>
<p><? if( has_post_thumbnail() ) {
the_post_thumbnail();
} ?></p>
<?php }
}
wp_reset_postdata();
In wp_postmeta table there is a _thumbnail_id associated with each post id. In admin, the images are showing as well.
What I am doing wrong?
Update
CPT registration code:
add_action('init', 'yrc_register_cpt_emfluence_platform_list', 0);
/**
* Create and register new Custom Post Type: Emfluence Platform List
* Fields: Name, Email, Platform List ID, Featured Image
*/
function yrc_register_cpt_emfluence_platform_list() {
$labels = array(
'name' => __('Platforms', 'yrc_csrvtool'),
'singular_name' => __('Platform', 'yrc_csrvtool'),
'menu_name' => __('Platforms', 'yrc_csrvtool'),
'all_items' => __('All Platforms', 'yrc_csrvtool'),
'view_item' => __('View Platform', 'yrc_csrvtool'),
'ad_new_item' => __('Add New', 'yrc_csrvtool'),
'add_new' => __('Add New', 'yrc_csrvtool'),
'edit_item' => __('Edit Platform', 'yrc_csrvtool'),
'update_item' => __('Update Platform', 'yrc_csrvtool'),
'search_items' => __('Search Platforms', 'yrc_csrvtool'),
'not_found' => __('Not found', 'yrc_csrvtool'),
'not_found_in_trash' => __('Not found in trash', 'yrc_csrvtool'),
);
$args = array(
'labels' => $labels,
'description' => __(' Platform List', 'yrc_csrvtool'),
'supports' => array('title', 'thumbnail' ),
'hierarchical' => false,
'public' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-email',
'show_in_nav_menus' => true,
'menu_position' => 5,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'post',
);
register_post_type('platform', $args);
}
And in the theme I have theme_support for thumbnails as well:
add_theme_support('post-thumbnails');
The images are very much visible in admin, but not when I use the_post_thumbnail() inside loop.
I have seen that, you have start php in wrong way before this bellow code
if( has_post_thumbnail() ) {
There is only <?, but it should be <?php
Please check your first code.
Thanks
The Problem is in your php syntax; <? if( has_post_thumbnail() ) { >> Here you are missing php after <? So nothing is interpreted inside this block of code by php. So Correct syntax is ....
<?php $query = new WP_Query(
array(
'post_type' => 'platform',
'status' => 'publish'
) );
if ( $query->have_posts() ) {
while ($query->have_posts()) {
$query->the_post(); ?>
<p><?php the_title(); ?></p>
<p><?php if( has_post_thumbnail() ) { //missing php after <? here
the_post_thumbnail();
} ?></p>
<?php }
}
wp_reset_postdata();

How to show custom post type posts according to category in wordpress

i have create custom post type for show plans and i want to show post according to category. i am using below code in functions.php for creating custom post type for plans.
add_action('init', 'create_post_types');
function create_post_types() {
register_post_type( 'numbers_plan',
array(
'labels' => array(
'name' => __( 'Numbers plan' ),
'singular_name' => __( 'Numbers plan' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'Numbers plan'),
)
);
// Add new taxonomy(like categories)
$labels = array(
'name' => _x( 'PlanCat', 'numbers_plan', 'textdomain' ),
'singular_name' => _x( 'PlanCat', 'numbers_plan', 'textdomain' ),
'search_items' => __( 'Search PlanCat', 'numbers_plan' ),
'all_items' => __( 'All PlanCat', 'numbers_plan' ),
'parent_item' => __( 'Parent PlanCat', 'numbers_plan' ),
'parent_item_colon' => __( 'Parent PlanCat:', 'numbers_plan' ),
'edit_item' => __( 'Edit PlanCat', 'numbers_plan' ),
'update_item' => __( 'Update PlanCat', 'numbers_plan' ),
'add_new_item' => __( 'Add New PlanCat', 'numbers_plan' ),
'new_item_name' => __( 'New PlanCat Name', 'numbers_plan' ),
'menu_name' => __( 'PlanCat', 'numbers_plan' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'numbers_plan' ),
);
register_taxonomy( 'numbers_plans', array( 'numbers_plan' ), $args );
}
for show plans i have create plan.php page with blow code.
<?php
$plan_group = get_terms( 'numbers_plans' );
?>
<?php
foreach ( $plan_group as $plan_group_term ) {
$plan_group_query = new WP_Query( array(
'post_type' => 'numbers_plan',
'tax_query' => array(
array(
'taxonomy' => 'numbers_plans',
'field' => 'slug',
'terms' => array( $plan_group_term->slug ),
'operator' => 'IN'
)
)
));
?>
<h2><?php echo $plan_group_term->name; ?></h2>
<ul>
<?php
if ( $plan_group_query->have_posts() ) : while ( $plan_group_query->have_posts() ) : $plan_group_query->the_post(); ?>
<div>
<div><?php echo the_title(); ?></div>
<div><?php the_field('plan_minutes'); ?></div>
<div><?php the_field('monthly_cost'); ?></div>
<div><?php the_field('cost_of_additional_minutes'); ?></div>
<?php echo do_shortcode("[ARForms_popup id=103 desc='Buy Now' type='link' height='540' width='800']"); ?>
<br/>
</div>
<?php endwhile; endif; ?>
</ul>
<?php
// Reset things, for good measure
$plan_group_query = null;
wp_reset_postdata();
}
?>
it is showing all categories posts but i want to show only one category and it's post. please tell me how can i do this.
One way to select posts from a certain category is by creating a small if statement around the post output. See the example below:
<?php
if (in_category('<category_name')) {
/* the post */
}
else {
/* Do nothing */
}
?>
A full example:
<?php if ( have_posts() ) :
if (in_category('Algemeen')) {
require_once('category-pages/Algemeen-category.php');
} elseif (in_category('Sport')) {
require_once('category-pages/Sport-category.php');
} elseif (in_category('Economie')) {
require_once('category-pages/Economie-category.php');
} elseif (in_category('Politiek')) {
require_once('category-pages/Politiek-category.php');
} elseif (in_category('Gemeente nieuws')) {
require_once('category-pages/Gemeente-category.php');
}
?>
In this example i check each post on it's category and load a different template file for the posts with the corresponding category
exclude all the othe categories id in array.
<?php
$args = array('exclude'=> array("Enter Other categories ID here")); //which categories you dont want
$$plan_group = get_terms('numbers_plans', $args);
?>
I Hope this will help you
<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php
the_title(); ?></a></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

Can't set post per page number for filterable portfolio in wordpress

I created a filterable portfolio using the tutorial found here:http://zoerooney.com/blog/web-development/filtering-portfolio-quicksand-taxonomies/#comments
Everything is working but I can not set my post per page. My portfolio goes by the number set in the Setting>Reading>Blog Pages Show at Most.
I tried using 'posts_per_page' =>'-1' but it does not work. Maybe I have it in the wrong place.
This is code in my functions file for my custom post type and categories for my portfolio items:
register_taxonomy("pftype", array("portfolio"), array("hierarchical" => true,"label" => "Project Types", "singular_label" => "Project Type"));
add_action('init', 'cptui_register_my_cpt_portfolio');
function cptui_register_my_cpt_portfolio() {
register_post_type('portfolio', array(
'label' => 'Portfolio',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'portfolio', 'with_front' => true),
'query_var' => true,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
'labels' => array (
'name' => 'Portfolio',
'singular_name' => 'Portfolio',
'menu_name' => 'Portfolio',
'add_new' => 'Add Portfolio',
'add_new_item' => 'Add New Portfolio',
'edit' => 'Edit',
'edit_item' => 'Edit Portfolio',
'new_item' => 'New Portfolio',
'view' => 'View Portfolio',
'view_item' => 'View Portfolio',
'search_items' => 'Search Portfolio',
'not_found' => 'No Portfolio Found',
'not_found_in_trash' => 'No Portfolio Found in Trash',
'parent' => 'Parent Portfolio',
)
) ); }
And this is the code I'm using for my portfolio page:
<?php
/**
* Template Name: Portfolio
*/
?>
<?php get_header(); ?>
<ul class="load-portfolio">
<li class="active">All</li>
<?php
$args = array( 'taxonomy' => 'pftype' );
$terms = get_terms('pftype', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '';
foreach ($terms as $term) {
$i++;
$term_list .= '<li>' . $term->name . '</li>';
if ($count != $i) $term_list .= ''; else $term_list .= '';
}
echo $term_list;
}
?>
</ul>
<ul class="portfolio-grid">
<?php
$pfportfolio = new WP_Query( 'post_type=portfolio' );
while ( $pfportfolio->have_posts() ) : $pfportfolio->the_post();?>
<?php
echo '<li data-id="post-'.get_the_ID().'" data-type="'.$terms_as_text = strip_tags( get_the_term_list( $post->ID, 'pftype', '', ' ', '' ) ).'">';
?>
<div class="item">
<div class="view third-effect">
<?php the_post_thumbnail( 'homepage-thumb' ); ?>
<?php
?>
<div class="mask">
</div>
<div class="item-title"><?php the_title(); ?></div>
</div>
</div>
<?php
echo '</li>';
endwhile;
wp_reset_postdata();
?>
</ul>
<?php get_footer(); ?>
I figured it out. I had to replace:
$pfportfolio = new WP_Query( 'post_type=portfolio' );
With this in my page template:
$pfportfolio = new WP_Query('paged=$paged&showposts=-1&'.$query."post_type=portfolio");

Resources