wp_query custom post types - wordpress

I dont know if I just can t see the forrest for the tress, but I need some help. I have a custom post type "business" with 2 related custom taxonomies, business_category and business_tags. On the custom archive template I have a custom search form:
<form class="search-form" role="search" method="get" action="<?php echo home_url( '/' ); ?>">
<div class="form-group">
<label for="search-input"><i class="fa fa-search" aria-hidden="true"></i><span class="screen-reader-text">Search icons</span></label>
<input type="hidden" name="post_type" value="wego_business" />
<input type="search" class="form-control search-field" placeholder="Search the Directory" value="" name="s" id="s">
</div>
</form>
This then display on a custom search template. I can not get results for the business_tags
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$tag = get_search_query();
$args = array (
's' => $s,
'order' => 'ASC',
'orderby' => 'name',
'tax_query' => array(
array(
'taxonomy' => 'business_tags',
'field' => 'slug',
'terms' => $tag,
),
),
'paged' => $paged
);
AM I missing something obvious?
Here is the cpt creation:
$labels = array(
'name' => _x( 'Business', 'Post Type General Name', 'wego_fran' ),
'singular_name' => _x( 'Business', 'Post Type Singular Name', 'wego_fran' ),
'menu_name' => __( 'Business', 'wego_fran' ),
'parent_item_colon' => __( 'Parent Business:', 'wego_fran' ),
'all_items' => __( 'All Business', 'wego_fran' ),
'view_item' => __( 'View Business', 'wego_fran' ),
'add_new_item' => __( 'Add New Business', 'wego_fran' ),
'add_new' => __( 'Add New', 'wego_fran' ),
'edit_item' => __( 'Edit Business', 'wego_fran' ),
'update_item' => __( 'Update Business', 'wego_fran' ),
'search_items' => __( 'Search Business', 'wego_fran' ),
'not_found' => __( 'Not found', 'wego_fran' ),
'not_found_in_trash' => __( 'Not found in Trash', 'wego_fran' ),
);
$args = array(
'label' => __( 'Business', 'wego_fran' ),
'description' => __( 'Business', 'wego_fran' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
'taxonomies' => array( 'business_category', 'business_tags' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-admin-home',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'business' ),
);
register_post_type( 'wego_business', $args );
<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$tag = get_search_query();
$args = array (
'post_type' => 'wego_business',
's' => $s,
'order' => 'ASC',
'orderby' => 'name',
'tax_query' => array(
array(
'taxonomy' => 'business_tags',
'field' => 'slug',
'terms' => $tag,
),
),
'paged' => $paged
);
// The Query
$wp_query = new WP_Query( $args );
$count = $wp_query->found_posts;
?>
the custom search template is search-wego_business.php using the above query

Perhaps you're missing the post_type field and you can also search inside the name field. A refactor would look like:
$args = array (
'post_type' => 'business',
's' => $s,
'order' => 'ASC',
'orderby' => 'name',
'tax_query' => array(
array(
'taxonomy' => 'business_tags',
'field' => 'name',
'terms' => $tag,
),
),
'paged' => $paged
);
Else, I would need more details - like: your taxonomy and custom post type declaration and the full source of your archive (at least the part that builds the search query).

Related

wordpress - query custom post type by category slug

I'm trying to query a custom post type by its category slug, but its not working at all, it always return blank - like no results
my custom post type definitions:
add_action( 'init', 'custom_post_albuns' );
// The custom function to register a movie post type
function custom_post_albuns() {
// Set the labels, this variable is used in the $args array
$labels = array(
'name' => __( 'Albuns' ),
'singular_name' => __( 'Album' ),
'add_new' => __( 'Add New Album' ),
'add_new_item' => __( 'Add New Album' ),
'edit_item' => __( 'Edit Album' ),
'new_item' => __( 'New Album' ),
'all_items' => __( 'All Albuns' ),
'view_item' => __( 'View Album' ),
'search_items' => __( 'Search Albuns' ),
'featured_image' => 'Featured Image',
'set_featured_image' => 'Add Featured Image'
);
// The arguments for our post type, to be entered as parameter 2 of register_post_type()
$args = array(
'labels' => $labels,
'description' => 'm6 Records Albuns',
'public' => true,
'menu_position' => 6,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
'has_archive' => true,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-album',
'query_var' => 'album'
);
// Call the actual WordPress function
// Parameter 1 is a name for the post type
// Parameter 2 is the $args array
register_post_type( 'album', $args);
}
add_action( 'init', 'create_album_taxonomies', 0 );
function create_album_taxonomies() {
$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 Name' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'hierarchical' => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'albuns' ),
);
register_taxonomy( 'albuns_categories', array( 'album' ), $args );
}
and my query:
$argsPost = array(
'post_type'=>'album',
'orderby'=>'date',
'order' => 'DESC',
'posts_per_page'=>'4',
'tax_query' => array(
array(
'taxonomy' => 'albuns_categories',
'field' => 'slug',
'terms' => array( 'ANYTERM' )
)
)
);
and I can't bring a list of albuns (if I remove the tax_query part, it returns to me all the "albuns" custom post type).
Do someone have any ideias???
Thanks!!
You can try
$argsPost = array(
'post_type'=>'album',
'orderby'=>'date',
'order' => 'DESC',
'posts_per_page'=>'4',
'tax_query' => array(
array(
'taxonomy' => 'albuns_categories',
'field' => 'slug',
'terms' => array( 'ANYTERM' ),
'operator' => 'IN'
)
)
);

Custom taxonomy loop not returning anythig

I've set up a custom taxonomy (menu_category) and custom post type (menu_item) in functions.php like this:
/* Set up custom post type for Menu Items */
function create_menu_items_post_type() {
$plugin_directory = plugins_url('images/', __FILE__ );
register_post_type( 'menu_item',
array(
'labels' => array(
'singular_name' => __( 'Menu item'),
'name' => __( 'The Menu'),
'add_new' => __( 'Add menu item'),
'add_new_item' => __( 'Add menu item'),
'edit_item' => __( 'Edit menu item'),
'new_item' => __( 'New menu item'),
'search_items' => __( 'Search menu items'),
'not_found' => __( 'No menu items found'),
'not_found_in_trash'=> __( 'No menu items found in trash'),
'all_items' => __( 'All menu items','sbr')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'menu_item'),
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'exclude_from_search' => true,
'query_var' => true,
'menu_position' => 27,
'can_export' => true,
'menu_icon' => get_template_directory_uri() . '/images/system/menu.svg',
'supports' => array('title', 'revisions', 'author'),
'capability_type' => 'post',
'taxonomies' => array('menu_category'),
'capabilities' => array(
'create_posts' => 'edit_posts',
),
'map_meta_cap' => true,
)
);
}
add_action( 'init', 'create_menu_items_post_type' );
/* Set up custom taxonomy for Menu Categories */
function create_menu_category_taxonomy() {
$labels = array(
'name' => _x( 'Menu categories', 'taxonomy general name' ),
'singular_name' => _x( 'Menu category', 'taxonomy singular name' ),
'search_items' => __( 'Search menu categories' ),
'all_items' => __( 'All menu categories' ),
'parent_item' => __( 'Parent menu categories' ),
'parent_item_colon' => __( 'Parent menu categories:' ),
'edit_item' => __( 'Edit menu category' ),
'update_item' => __( 'Update menu category' ),
'add_new_item' => __( 'Add new menu category' ),
'new_item_name' => __( 'New menu category' ),
'menu_name' => __( 'Menu categories' ),
);
register_taxonomy(
'menu_category',
'menu_item',
array(
'label' => __( 'Menu Category' ),
'rewrite' => array( 'slug' => 'menu_category' ),
'hierarchical' => true,
'labels' => $labels
)
);
}
add_action( 'init', 'create_menu_category_taxonomy' );
...and that works fine when I'm in WP admin - I can add new categories, posts etc.
But on the front-end I'm trying to return a list of all the menu_category categories and menu_item posts like this:
$the_menus = get_categories(array(
'echo' => 0,
'hide_empty' => false,
'taxonomy' => 'menu_category',
'hierarchical' => 1,
'show_count' => 0
)); ?>
<?php
foreach ($the_menus as $the_menu) {
$the_menu_args = array(
'posts_per_page' => -1,
'post_type' => 'menu_item',
'showposts' => -1,
'post_status' => 'publish',
'cat' => $the_menu->cat_ID
);
$term = get_queried_object();
$the_menu_tasks = new WP_Query($the_menu_args);
$the_menu_slug = $the_menu->slug;
$the_menu_ID = $the_menu->cat_ID;
$page_slug = $term->slug;
?>
<li>
<a href="<?php echo get_category_link( $the_menu_ID ); ?>">
<?php echo $the_menu->cat_name; ?>
</a>
</li>
<?php
}
wp_reset_postdata();
The problem is this loop returns nothing.
If I remove 'taxonomy' => 'menu_category' from $the_menus and also remove 'post_type' => 'menu_item' from $the_menu_args, it returns all the normal categories and posts (you know, regular posts and categories).
So it seems it's only failing when I specify the custom taxonomy and custom post type.
What am I doing wrong?
PS: The 'menu items' mentioned here are for a restaurant menu I'm trying to build. It has nothing to do with the WordPress menu :-P
To get the posts from custom taxonomy we need to pass tax query in arguments.
Try the below code. It works fine on my end.
<?php
$the_menus = get_terms( array(
'taxonomy' => 'menu_category',
'hide_empty' => false,
));
foreach ($the_menus as $the_menu) {
$args = array(
'post_type' => 'menu_item',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'menu_category',
'field' => 'term_id',
'terms' => $the_menu->term_id,
)
),
);
$term = get_queried_object();
$the_menu_tasks = new WP_Query($args);
$the_menu_slug = $the_menu->slug;
$the_menu_ID = $the_menu->term_id;
$page_slug = $term->slug;
?>
<!-- get category names here -->
<li>
<a href="<?php echo get_term_link( $the_menu_ID ); ?>">
<?php echo $the_menu->name; ?>
</a>
</li>
<!-- get category names here //END -->
<?php
/* GET posts according to custom taxonomy id */
if ($the_menu_tasks->have_posts()){
while($the_menu_tasks->have_posts()) : $the_menu_tasks->the_post();
echo get_the_title() .'<br />';
endwhile;
}else{
echo "Sorry! Posts not found";
}
/* GET posts according to custom taxonomy id -- END */
}
?>

Wordpress Show custom post type specific category in post page?

I am new in development. I have created a custom post type manually, which is as in the code given below. I am unable to get post form specific category (Logo, Website) in Custom post type. Help me to solve this.
function my_custom_taxonomies(){
$lables = array(
'name' => 'Type',
'singular_name' => 'my_custom_taxonomiesType',
'all_items' => 'All Types',
'add_new_item' => 'Add Type',
'edit_item' => 'Edit Type',
'search_item' => 'Search Type',
'parent_item' => 'Parent Type',
'parent_item_colon' => 'Parent Type',
'update_item' => 'Update Type',
'new_item_name' => 'New Type Name',
'menu_name' => 'Type'
);
$args = array(
'labels' => $lables,
'query_var' => ture,
'rewrite' => array('slug' => 'type'),
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
);
register_taxonomy('type', array('portfolio'), $args);
}
add_action('init','my_custom_taxonomies');
I am trying this code, but cannot get it to work.
$args = array( 'post_type' => 'portfolio', 'category_name' => 'logo', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<ul>
<li><?php the_title(); ?></li>
</ul>
<?php echo '<div class="entry-content">';
//the_content();
echo '</div>';
endwhile;?>
Just Use this as you have registered taxonomy. You can refere about WP_Query here
$query = new WP_Query( array(
'post_type' => 'portfolio',
'tax_query' => array(
array (
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'logo',
)
),
) );

Custom taxonomy list in WordPress

I just registered following code in my function
add_action( 'init', 'create_resources_taxonomies', 0 );
function create_resources_taxonomies() {
$labels = array(
'name' => _x( 'cat2', 'taxonomy general name' ),
'singular_name' => _x( 'cat2', 'taxonomy singular name' ),
'search_items' => __( 'Search category' ),
'all_items' => __( 'All category' ),
'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 Name' ),
'menu_name' => __( 'genre' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'resources' ), $args );
}
and i want to get category list in my sidebar, so i added following code in my sidebar
<?php
$taxonomy = 'genre';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
?>
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
But i just get "No categories" text. What is wrong with my code?
Your code looks correct, I guess your categories are empty! Am i correct? if yes, add the following code to your last array section before 'title_li'
'hide_empty' => 0,
So your code should be like this
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'hide_empty' => 0,
'title_li' => $title

Wordpress Taxonomy Category Filter

Hi my english very very bad sorry:)wordpress custom post name=realestate taxonmy name=realestate_cat created.I want created a post list of the content based on taxonomy categories
My codes
<?php
$args = array(
'post_type'=> 'realestate',
'taxonomy' => 'realestate_cat',
'cat' => 2,
);
query_posts( $args );
if (have_posts()) : while (have_posts()) : the_post();
?>
This code does not work...
My function code:
function themes_taxonomy() {
register_taxonomy(
'emlak_kategori', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'emlak', //post type name
array(
'hierarchical' => true,
'label' => 'Emlak Kategorileri', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'emlak', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'themes_taxonomy');
function filter_post_type_link( $link, $post) {
if ( $post->post_type != 'emlak' )
return $link;
if ( $cats = get_the_terms( $post->ID, 'emlak_kategori' ) )
$link = str_replace( '%emlak_kategori%', array_pop($cats)->slug, $link );
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
add_action( 'init', 'register_themepost', 20 );
function register_themepost() {
$labels = array(
'name' => _x( 'Emlaklar', 'catchthemes_custom_post','catchthemes' ),
'singular_name' => _x( 'Theme', 'catchthemes_custom_post', 'catchthemes' ),
'add_new' => _x( 'Emlak Ekle', 'catchthemes_custom_post', 'catchthemes' ),
'add_new_item' => _x( 'Yeni Bir Emlak Ekle', 'catchthemes_custom_post', 'catchthemes' ),
'edit_item' => _x( 'Emlak İlanını Düzenleyin', 'catchthemes_custom_post', 'catchthemes' ),
'new_item' => _x( 'Yeni Emlak İlanı', 'catchthemes_custom_post', 'catchthemes' ),
'view_item' => _x( 'Emlak İlanınızı Önizleyin', 'catchthemes_custom_post', 'catchthemes' ),
'search_items' => _x( 'Emlak Ara', 'catchthemes_custom_post', 'catchthemes' ),
'not_found' => _x( 'Henüz Emlak İlanı Eklenmemiş', 'catchthemes_custom_post', 'catchthemes' ),
'not_found_in_trash' => _x( 'Çöp Kutusunda Birşey Bulunamadı', 'catchthemes_custom_post', 'catchthemes' ),
'parent_item_colon' => _x( 'Parent ThemePost:', 'catchthemes_custom_post', 'catchthemes' ),
'menu_name' => _x( 'Emlaklar', 'catchthemes_custom_post', 'catchthemes' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Emlaklar',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
'taxonomies' => array( 'post_tag','emlak_kategori'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => get_stylesheet_directory_uri() . '/images/maidenstower.png',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'emlak/%emlak_kategori%','with_front' => FALSE),
'public' => true,
'has_archive' => 'emlak',
'capability_type' => 'post'
);
register_post_type( 'emlak', $args );//max 20 charachter cannot contain capital letters and spaces
}
you must try with the taxonomy term like below
<?php
$args = array(
'post_type'=> 'realestate',
'taxonomy' => 'realestate_cat',
'term'=>'your taxonomy term'
'cat' => 2,
);
query_posts( $args );
if (have_posts()) : while (have_posts()) : the_title (); the_post();
?>

Resources