Get categories query not working in function.php - php

I am trying to get the categories of my custom post type in functions.php But it not return any value, when i run this query in any theme file it work fine. Here is my code
function get_destinations(){
$args = array(
'type' => 'accomodation',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'facilitie',
'pad_counts' => false
);
$categories = get_categories($args);$destinations = array();
foreach ($categories as $cat) {
if($cat->cat_name != ''){
$destinations[$cat->cat_name] = $cat->cat_name;
}
}
return $destinations;
}
I am using this code to add meta field, now i have to pass the category to select tag which is
$my_meta2->addSelect($prefix.'select_field_id',get_destinations(),array('name'=> 'Select Destination'));
The original code is like this, they pass the value in array.
$my_meta->addSelect($prefix.'select_field_id',array('selectkey1'=>'Select Value1','selectkey2'=>'Select Value2'),array('name'=> 'My select ', 'std'=> array('selectkey2')));
But not getting any value, Any idea where i am wrong. Thanks

As far as i know, get_categories() won't accept a post_type argument. You probably should use taxonomy for that.
You can find more info here:
https://codex.wordpress.org/Function_Reference/get_categories

Related

How to get all post categories without custom post type categories?

I am creating a Blog in wordpress.
I have a list of categories:
Technology,
Art,
fashion,
Home,
Lifetime,
Education,
Business,
Religion,
Design and home,
Marketing
In which some of these categories I am using only in Custom Post Type (Technology, Art, Fashion) and others only in Normal Posts (Home, Life, Education, Business, Religion, Design and home, Marketing).
Now I need to get the list of categories that are only being used from normal posts to show them on my blog ().
I tried to do the following but it returns all categories including CPTs:
$categories = get_categories();
foreach($categories as $category) {
echo '<li class="cat-name" . '>' . $category->name . '</li>';
}
I just need to show the categories:
Home, Life, Education, Business, Religion, Design & Home, Marketing.
And exclude those that are being used in CPT.
Please Help!
You need to pass in the arguments and exclude the term_ids or include on the term_ids you want. You can use one of the following: 'exclude', 'exclude_tree', or 'include'.
$args = array(
'taxonomy' => 'category',
'exclude' => array(65,23,98,23,78), // term_ids you want to exclude
'exclude_tree' => (65,23,98,23,78), // term_ids you want to exclude and their descendants/children
'include' => (11,51,90,57,29), // only the term_ids you want to include
'orderby' => 'name',
'order' => 'ASC',
"hide_empty" => 1,
);
$cats = get_categories($args);
foreach ($cats as $cat){
echo $cat_slug = $cat->slug;
}
'include'
Array or comma/space-separated string of term IDs to include. Default empty array.
'exclude'
Array or comma/space-separated string of term IDs to exclude. If $include is non-empty, $exclude is ignored. Default empty array.
'exclude_tree'
Array or comma/space-separated string of term IDs to exclude along with all of their descendant terms. If $include is non-empty, $exclude_tree is ignored. Default empty array.
Learn more about all the arguments you can pass to get_categories() or get_terms(), get_posts() etc
https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
Try WP_Query if the above isn't working
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'home', 'life', 'education', 'business', 'religion', 'design-and-home', 'marketing' ),
'operator' => 'IN'
),
array(
'taxonomy' => 'custom_taxonomy_registerd_to_cpt_slug', // again make sure in your cpt plugin that this taxonomy is unique and not just 'category'
'field' => 'slug',
'terms' => array( 'technology', 'art', 'fashion' ),
'include_children' => false,
'operator' => 'NOT IN'
)
)
);
$cats = new WP_Query($args);
foreach ($cats as $cat){
print_r($cat); // use this to find the values you need. Remove after you build the link html
}
This function with shortcode is tested and work for me
<?php
function list_categories_func($atts) {
$a = shortcode_atts( array('' => '',), $atts );
$args = array('taxonomy'=>'category','parent'=>0,'orderby'=>'name','order'=>'ASC', 'hide_empty'=>0,);
$categories=get_categories($args);
echo "<ul>";
foreach($categories as $category){
$name=$category->slug;
echo "<li>$id</li>";
}
echo "</ul>";
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'ni_categories_row', 'list_categories_func' );
?>

WordPress - WP_QUERY limit findings to posts with exact same values

I am facing some issues with my custom taxonomies in one of my custom post types : I have a page that retrieves my custom posts that have the same taxonomy fields as the page.
For example, one of the custom taxonomies shared between the page and the CPT is 'categorie'. Using a tax_query field in a WP_QUERY, I am able to retrieve the posts that do have the same categorie fields checked as my page's, but I can't seem to find how to get posts that have the EXACT same values.
Here are some visuals in case you're confused :
Page taxonomy with values checked
CPT taxonomy with values checked
So they both have the 'chauffage' value checked and no other one.
Let's say now that I have an other custom post that has 'chauffage' checked, but also 'sanitaire'. How do I tell my WP_QUERY not to retrieve it ?
I've tried to use the operator 'AND' inside of my tax_query array (since it is 'IN' by default) but it didn't work.
The idea I had now was to retrieve all of the taxonomy values and say that the unchecked ones use the operator 'NOT IN', which would work I guess if only I managed to do so. I am only able to get the checked ones, even when using wp_get_post_terms with 'hide_empty' set to 0.
Here's my code :
$page_id = $wp_query->get_queried_object()->ID;
$page_category = wp_get_post_terms($page_id, 'categorie', array(
'hide_empty' => 0
));
// var_dump($page_category);
$page_client = wp_get_post_terms($page_id, 'client', array(
'hide_empty' => 0
));
if($page_category) {
$buffer = array();
foreach ($page_category as $category) {
array_push($buffer, $category->slug);
}
$page_category = $buffer;
}
// var_dump($page_client);
if($page_client) {
$buffer = array();
foreach ($page_client as $client) {
array_push($buffer, $client->slug);
}
$page_client = $buffer;
}
$tax_query = array(
'relation' => 'AND'
);
array_push($tax_query, array(
'taxonomy' => 'categ_article_activite',
'field' => 'slug',
'terms' => $page_category,
'operator' => 'AND'
));
// var_dump($page_client);
array_push($tax_query, array(
'taxonomy' => 'client_article_activite',
'field' => 'slug',
'terms' => $page_client,
'operator' => 'AND'
));
// var_dump($tax_query);
$args = array(
'post_type' => 'article_activite',
'meta_key' => '_article_principal',
'posts_per_page' => 1,
'meta_value' => 'on',
'tax_query' => $tax_query,
'order' => 'ASC'
);
$query = new WP_Query($args);
EDIT : I forgot to mention, I also thought about using the operator 'NOT IN' for the values I do not want to retrieve but since the people that are going to use the website may add taxonomy values, it would require to go in the code add this specific value which is not an option

$wpdb get_results - loop from specific category

I have this code, its working good, but its gets posts from all categories. I need to get posts only from category ID 2. I tried to add "AND term_taxonomy_id = 2" to my code, but this didint work. Can somebody help me with this, my code:
<?php //loops all posts
$my_query = $wpdb->get_results
("SELECT * FROM `{$table_prefix}posts`, {$table_prefix}postmeta
WHERE {$table_prefix}posts.post_status = 'publish'
AND {$table_prefix}posts.id = {$table_prefix}postmeta.post_id
AND {$table_prefix}postmeta.`meta_key` = 'wpcf-data-nuo' ORDER BY if({$table_prefix}postmeta.`meta_value` = '' or {$table_prefix}postmeta.`meta_value` is null,1,0), {$table_prefix}postmeta.`meta_value` ASC LIMIT 12");
foreach($my_query as $post) {
setup_postdata($post);
?>
Please follow below code, Hope! its working
You can define the meta key for orderby parameter using the old method (I tested on WP 3.1.1)...
query_posts(
array( 'post_type' => 'services',
'order' => 'ASC',
'meta_key' => 'some_key',
'orderby' => 'meta_value', //or 'meta_value_num'
'meta_query' => array(
array('key' => 'order_in_archive',
'value' => 'some_value'
)
)
)
);
OR
For me, I wanted to order by a numeric field and I had to use 'type' => 'NUMERIC' inside the meta query.
This issue in general is cleared up in WordPress 4.2 by using named queries. e.g.
$args = array(
'post_type' => 'services',
'orderby' => 'order_clause',
'meta_query' => array(
'order_clause' => array(
'key' => 'order_in_archive',
'value' => 'some_value',
'type' => 'NUMERIC' // unless the field is not a number
)));
Reference link for set the meta value in query: https://rudrastyh.com/wordpress/meta_query.html
You can do it by WP_QUERY instead writing custom Query.
The query to get the results and then your custom sort order by altering the WP_QUERY if default order parameters not work as expected.
$args = array(
'post_type' => 'post',
'posts_per_page' => 8,
'cat' => 2,
'meta_key' => 'wpcf-data-nuo',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
add_filter('posts_orderby', 'filter_query');
$q = new WP_Query($args);
remove_filter('posts_orderby', 'filter_query');
function filter_query( $orderby_statement ) {
global $table_prefix;
$orderby_statement .= " if({$table_prefix}postmeta.`meta_value` = '' ";
$orderby_statement .= " or {$table_prefix}postmeta.`meta_value` is null,1,0), {$table_prefix}postmeta.`meta_value`";
return $orderby_statement;
}
Note: Do not remove spaces from $orderby_statement .= because it require spaces while running query.
If it still doesn't work, then add var_dump($orderby_statement); before return $orderby_statement; then copy the SQL and add it in your question so it will help us to understand what is the issue.

Wordpress count private posts in wp_list_categories

Since I didn't find anything helpful on the web I'd like to ask:
In WP you can with wp_list_categories and show_count=1 and hide_empty=0 list all categories (even empty ones) with the number of posts at the end.
example output: General (9)
So when I make a private post and publish it the count for General remains on 9.
Is there a way I can also show the count of private posts?
Well I found quite a solution for this:
$args = array(
'include' => array( 8,9,10 ), //categories that contains the private articles
'title_li' => '',
'show_count' => 0,
'hide_empty' => 0,
);
$categories = get_categories($args);
foreach($categories as $category) {
$args_priv = array(
'cat' => $category->term_id,
'post_status' => array( 'private','publish' ),
);
$the_query = new WP_Query( $args_priv );
$count = '('. $the_query->found_posts .')';
echo '' . $category->name.' '.$count.'<br />';
}

Get an array of child posts IDs, wordpress 3.0, php

Ok here's one for ya...
On a custom template I'm using this code to retrieve & display a list of child pages/posts
$args = array(
'depth' => 1,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => $post->ID,
'exclude' => '',
'include' => '',
'title_li' => '',
'echo' => 1,
'authors' => '',
'sort_column' => 'menu_order, post_title',
'link_before' => '',
'link_after' => '',
'walker' => '' );
wp_list_pages( $args );
This works great, I'm also wondering how I can access/create an array of child post ID's. My goal is to access some custom fields meta data through the get_post_meta() function of each child post using it's ID.
Thanks guys.
I guess I wasn't very clear with this one as it's the first time I've never recieved an answer from SO.
I managed to find the information I needed and will place it here for anyone else browsing with the same request.
ok - To get all child IDs..
$pages = get_pages('child_of=X');
foreach($pages as $child) {
// Now you have an object full of Children ID's that you can use for whatever
// E.G
echo $child->ID . "<br />";
}
If you want to build an array of post ids for later use you can do this:
$pageids = array();
$pages = get_pages('child_of=X');
foreach($pages as $page){
$pageids[] = $page->ID;
}
And you have a clean array of just page ids.
$children = get_posts('post_parent=SLUG_OF_PARENT_POST&post_status=publish');
foreach($children as $child)
{
echo '<br/>ID:'.$child->ID;
}
you can use other attributes (i.e. $child->post_content)...
if you need to define post_type, then add this argument too : &post_type=POST_TYPE_NAME
Another way to do this:
$my_page_id = 12345;
$child_query_args = array(
'post_parent' => $my_page_id,
'post_type' => 'page',
'posts_per_page' => -1,
'fields' => 'ids',
);
$child_query = new WP_Query($child_query_args);
if ( $child_query && $child_query->have_posts() && $child_query->posts ) {
// (Since fields=ids, $child_query->posts is just an array of IDs)
$child_ids = $child_query->posts;
foreach ( $child_ids as $child_id ) {
$whatever = get_post_meta( $child_id, 'whatever', true );
echo esc_html($whatever);
}
}

Categories