I'm using get_pages in a Wordpress theme to create the nav.
I have some pages I don't want to show in the nav.
On all pages I have a 'dont_show_in_nav' true/false custom field.
I can use meta_key to add pages that have 'dont_show_in_nav' selected
but I'd like to not show the pages that have 'dont_show_in_nav' selected.
I could create a 'show_in_nav' custom field and select all the pages to show but I have to many pages to do that.
I've tried 'meta_value' => false
$pages_args = array(
'sort_column' => 'menu_order',
'parent' => 0,
'post_type' => 'page',
'post_status' => 'publish',
'meta_key' => 'dont_show_in_nav',
'meta_value' => true
);
I'm guessing that the issue is that meta_value has to be a string since WordPress only supports strings for post meta. When you create your field, what are you actually saving? Is it the word "true", is it a '0', a '1' etc?
Regardless, a much better method for generating nav menus in wordpress is to take advantage of the nav menu functions. Take a look at wp_nav_menu(), that should work much more reliably than a meta key.
As of now Wordpress documentation for get_pages() says the function doesn't support querying with a meta_key:
NOTE: This function will currently not retrieve pages using the 'meta_key' and 'meta_value' parameters in the $args array as described below. Until this is fixed in the WordPress codebase, you may want to use get_posts() instead.
So your code should call get_posts() instead:
$pages_args = array(
'orderby' => 'menu_order',
'parent' => 0,
'post_type' => 'page',
'post_status' => 'publish',
'meta_key' => 'dont_show_in_nav',
'meta_value' => 'true'
);
$pages = get_posts($pages_args);
Note that I quoted 'true', since to Wordpress it's just a text field. Also, some arguments for get_posts are slightly different than for get_pages.
Related
I am using Search and Filter Pro plugin to fetch search result based on various post meta and taxonomies, I am not completely using the plugin's entire interface only its custom query approach:
https://searchandfilter.com/documentation/search-results/custom/
I attached search and filter to the WP_Query and that part looks like this:
$premium_args = array(
"s" => $_GET['q'],
'posts_per_page' => 20,
'paged' => 1,
'post_type' => array( 'contactdirectory'),
'orderby' => 'title',
'order' => 'ASC',
);
$premium_args['search_filter_id'] = 166;
And in the plugin I have set the 'Field relationships:' to AND for that search and filter instance.
However it's not working as expected, here's an example:
https://indiadairy.com/?s=&p_type=cd&_sft_Industries=equipment-manufacturers&_sfm_state=Maharashtra
_sft_Industries=equipment-manufacturers is there, and then _sfm_state=Maharashtra but in the search results you can see there are results which has a different value for Industries taxonomy like Dairy Plants and 'NGO' whereas it should only return results that satisfies the value passed to both parameters.
I tried adding
'meta_query' => array(
'relation' => 'AND',
)
It doesn't work, which I expected, as it's for meta queries passed manually in the WP_Query arguments.
I have an import-script for custom post-type contents. I need to add some categories to the posts. But after insert the categories are not added to the post (custom post type 'memember_pages')
My insert-statements:
$insertargs = array(
'post_author' => $user_id,
'post_content' => $page->beschreibung,
'post_title' => $page->name,
'post_status' => 'publish',
'post_category' => array(7,14),
'post_type' => 'member_pages'
);
$insertresult = wp_insert_post($insertargs,true);
$insertresult returns the new post->ID, no errormessage.
The category-ID's exists of course.
The insert works, but without the categories.
The values 7,14 are only examples, which shows that I use (existing) ID's
I tried wp_set_post_categories too, but it also does not work.
wp_set_post_categories returns array(0) when using it.
Any idea to add categories to cpt?
thanks,
rudi
I have a custom post type clientgallery and the custom taxonomy client.
To get all galleries I can type website.com/clientgallery.
But I want to show only galleries of a specific client, like: website.com/clientgallery/miller
So, miller should act like a get parameter.
I already know how to get the galleries by client, but I don't know how to get the parameter part working.
$args = array(
'numberposts' => -1, //limit the number of posts, set 0 if no limit required.
'orderby' => 'post_date', //order by post_date field.
'order' => 'DESC', //order by descending oder.
'post_type' => 'clientgallery', //the post type is custom post type 'News & Events'
'post_status' => 'publish', //post status is 'publish'
'tax_query' => array(
array(
'taxonomy' => 'client', //custom taxonomy slug
'field' => 'slug', //select taxonomy term by slug
'terms' => $_GET['client'] //taxonomy term is called 'home-page'
)
));
Call it taxonomy-client.php :
See all info here on template hierarchy for WordPress : http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
If your taxonomy client is only associated with the clientgallery post type, then website.com/client/miller should suffice to show your list of clientgalleries for this client. I tested it on my site, it works. Or am I getting something wrong?
Basically I have a custom post type of 'products' which has two taxonomies attached to it...the normal 'category' and a custom taxonomy called 'brands'.
I have a page which is 'brand' specific. On this page I'd like to list all the 'categories' that have a 'product' in them with a term of the 'brand' whos page I'm on attached.
Eg. say I'm on the "Nike" page. I want it to list all categories that have a 'product' in them with the 'brand' of "Nike" attached to them.
My initial thoughts are to use get_categories but theres now way to define a specific taxonomy or 'brand'?
$categories = get_categories('orderby=name&depth=1&hide_empty=0&child_of='.$cat);
Anyone done this before or knows a way to query the database directly to get the required results?
Any help is much appreicated, Thanks
I realize this is an older question, but in case anyone stumbles across this question while looking for an answer (as I did), get_categories() will now do this natively. Note the 'taxonomy' => 'taxonomy-type' in the $args array. Simply provide the registered taxonomy name to override the default value of category.
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false );
http://codex.wordpress.org/Function_Reference/get_categories
sorry about the delay. managed to sort it on the Wordpress stack exchange at the following link (for anyone who has the same issue): https://wordpress.stackexchange.com/questions/10998/get-categories-for-custom-post-type-with-a-specific-custom-taxonomy-attached
To my knowledge, you cant use the get_categories () function with "Custom Post Types" and "Custom Taxonomies".
On this site you can find a good tutorial in how to use "Custom Taxonomies"
http://net.tutsplus.com/tutorials/wordpress/introducing-wordpress-3-custom-taxonomies/.
Under the point "Displaying Taxonomy Classifications on Individual Pages" there would have to the solution for your problem.
I've got a custom post type registered, with custom taxonomies, everything good and clear.
I wish I could somehow display all the categories of the taxonomy with pagination.
I am using a custom category Walker and am thinking to register a custom page rewrite for page query, and add some code to the category walker to display only the desired interval. Am I on the right direction?
Also, wp_list_categories sends to the category Walker the entire list of categories. Is there any way to get only the desired interval?
In this particular setup, no.
But I found a workaround: I registered a function that created a custom-type post each time I added a category in the taxonomy. That way I used the archive feature for custom post types, made available in 3.1-RC1.
function create_crew_post_on_term($term_id) {
$term = get_term($term_id, 'crew');
$post = array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_author' => 1,
'post_content' => '',
'post_date' => date('Y-m-d H:i:s'),
'post_excerpt' => '',
'post_name' => $term->slug,
'post_status' => 'publish',
'post_title' => $term->name,
'post_type' => 'crew'
);
wp_insert_post( $post );
}
add_action('created_crew', 'create_crew_post_on_term');