Invalid Argument Error - php

Im getting an error at the following URL:
http://www.greenmonkeypublicrelations.com/citypads/apartments/the-ivinghoe-2-bedroom-1-bathroom-apartment/
The purpose here is to identify if the post has children, if it has children, to list the terms of those children and echo the permalink to the post as the href and echo the term title as the text to show. Also if the post is a child, then to get the parent post and list the same to show the links on both levels.
Heres my code:
<?php
if( has_children() ){
$postid = get_the_ID();
$args = array(
'post_parent' => $postid,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
$children_array = get_children( $args, OBJECT );
foreach ($children_array as $child) {
$terms = get_the_terms( $child->ID, 'Apartment_type' );
foreach ($terms as $term) {
echo '' . $term->name . '<br/>';
}
}
} elseif (!has_children()) {
if ( wp_get_post_parent_id( get_the_id() ) ){
$postid = wp_get_post_parent_id( get_the_id() );
$args = array(
'post_parent' => $postid,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
$children_array = get_children( $args, OBJECT );
foreach ($children_array as $child) {
$terms = get_the_terms( $child->ID, 'Apartment_type' );
foreach ($terms as $term) {
echo '' . $term->name . '<br/>';
}
}
}
} elseif( ( !has_children() ) && (!get_post_ancestors( $post->ID ))) {
echo 'on its own';
} ?>

The problem is that you expect $children_array or $terms are always arrays. foreach expects an array, so if the $children_array or $terms have the value (i.e.) false you'll get this error.
Can you try this:
<?php
if( has_children() ){
$postid = get_the_ID();
$args = array(
'post_parent' => $postid,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
$children_array = get_children( $args, OBJECT );
if( is_array( $children_array ) ) {
foreach ($children_array as $child) {
$terms = get_the_terms( $child->ID, 'Apartment_type' );
if( is_array( $terms ) ) {
foreach ($terms as $term) {
echo '' . $term->name . '<br/>';
}
}
}
}
} elseif (!has_children()) {
if ( wp_get_post_parent_id( get_the_id() ) ){
$postid = wp_get_post_parent_id( get_the_id() );
$args = array(
'post_parent' => $postid,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
$children_array = get_children( $args, OBJECT );
if( is_array( $children_array ) ) {
foreach ($children_array as $child) {
$terms = get_the_terms( $child->ID, 'Apartment_type' );
if( is_array( $terms ) ) {
foreach ($terms as $term) {
echo '' . $term->name . '<br/>';
}
}
}
}
}
} elseif( ( !has_children() ) && (!get_post_ancestors( $post->ID ))) {
echo 'on its own';
} ?>

Related

I want to print the meta_value of all with the key "post_views_count" from the post_meta table

I want to print (meta_value) from all fields
"Post_views_count" from table (post_meta) But provided that this happens in every category.
I want to count every visit inaide selected category , and in the end appear the name of category and every category generate visitors inside it .
This is the code that i used it and work good but the page are too slowly due to recurrent queries
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'depth' => 0
);
// Extract all categories
$categories = get_categories( $args );
foreach ( $categories as $category ) {
// Extract all posts within each category
$args = array(
'cat' => $category->term_id,
'orderby' =>'post_date',
'post_type' => 'post',
'posts_per_page' => '-1',
);
$query = new WP_Query( $args );
// Views variable
$allview_cat = 0;
if ( $query->have_posts() ) {
$posttype = get_post_type(get_the_ID());
while ( $query->have_posts() ) {
$query->the_post();
$do_not_duplicate[] = $post->ID;
// Query that extracts all fields containing post_views_count + post id
$post_id = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE (meta_key = 'post_views_count' AND post_id = $post->ID );");
$post_id = array_map(function($item){
return $item->{'meta_value'};
}
,$post_id);
foreach($post_id as $m_key => $m_value) {
$allview_cat = $m_value + $allview_cat ."<br>";
}
} // end while
} // end if
// Views variable
echo $allview_cat;
}
?>
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'depth' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$cat_id = $category->term_id;
$args_post = array(
'cat' => $cat_id,
'post_type' => 'post',
'posts_per_page' => '-1',
);
$query = new WP_Query( $args_post );
$allview = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
if ( !isset( $allview[$cat_id] ) {
$allview[$cat_id] = 0;
}
$post_views_count = get_post_meta( $post_id, 'post_views_count' );
$allview[$cat_id] = (int)$allview[$cat_id] + (int)$post_views_count;
}
}
}
//display results
foreach ( $allview as $cat => $value ) {
echo $cat . ' = ' . $value;
}
?>

Woocommerce Display Termmeta

I have this code to display term metadata but for some reason, it's not showing up / Data is blank Sorry for not making it clear the first time
UPDATED
if ( 'new_column2' == $column_name ) {
/*Begin*/
foreach((get_term_meta( $post->ID, 'crosssell' )) as $category) {
$cat_name = $category->name . '</br>';
$cat_id = $category->term_id . '</br>';
$crosssell = get_term_meta( $cat_id, 'crosssell', true );
}
/*End*/
$value = $crosssell;
}
return $value;
}
what am i doing wrong
You're assigning variables but not actually outputting anything.
foreach( ( get_term_meta( $post->ID, 'crosssell' ) ) as $category ) {
echo $category->name . '</br>';
echo $category->term_id . '</br>';
echo get_term_meta( $category->term_id, 'crosssell', true );
}
This fixed the problem I had.
if ( 'new_column2' == $column_name ) {
/*Begin*/
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
//$woo_categories = get_categories( $prod_cat_args );
$woo_categories = get_the_terms( $post->ID, 'product_cat' );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
}
$cat_term_id = $woo_cat_id;
$crosssell = get_term_meta( $cat_term_id, 'crosssell', true );
/*End*/
$value = $crosssell;
}
return $value;
}

Get custom taxonomy's custom meta from outside the loop

I've created a number of custom fields for custom taxonomy. Got no problem pulling their values inside the query. But in this case, I get them repeating as many times as I have posts in this taxonomy.
This is the code with it inside the loop
$tables_terms = $atts['custom'];
$tabargs = array(
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'customtables',
'tax_query' => array(
array(
'taxonomy' => 'tables',
'field' => 'slug',
'terms' => array(
$tables_terms
)
)
)
);
$tabs = new WP_Query( $tabargs );
if( $tabs->have_posts() ){
while( $tabs->have_posts() ) : $tabs->the_post();
$terms = get_the_terms( get_the_ID(), 'tables' );
foreach ( $terms as $term ) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
echo $term_meta['term_1'];
}
endwhile;
wp_reset_postdata();
echo $custom;
}
How do I get these to show once, outside the loop?
You can use, get_term_meta;
get_term_meta retrieves metadata for a term.
get_term_meta( $term->term_id, 'your_term_name', true )
So the following code actually worked for me
$term_slug = $tables_terms;
$taxonomies = get_taxonomies();
foreach ( $taxonomies as $tax_type_key => $taxonomy ) {
if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) {
break;
}
}
$t_id = $term_object->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
and from there on, I simply echoed each meta like this
echo $term_meta['term_1'];
Try Like that:
Store your output in a array and check when store it that value already exist or not.
$tables_terms = $atts['custom'];
$tabargs = array(
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'customtables',
'tax_query' => array(
array(
'taxonomy' => 'tables',
'field' => 'slug',
'terms' => array(
$tables_terms
)
)
)
);
$tabs = new WP_Query( $tabargs );
$term_meta_array = array();
if( $tabs->have_posts() ){
while( $tabs->have_posts() ) : $tabs->the_post();
$terms = get_the_terms( get_the_ID(), 'tables' );
foreach ( $terms as $term ) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
echo $term_meta['term_1'];
if(!in_array($term_meta['term_1'], $term_meta_array)) array_push($$term_meta_array, $term_meta['term_1']);
}
endwhile;
wp_reset_postdata();
echo $custom;
foreach($term_meta_array as $st){
echo $st;
}
}

How to search for a string in a category name?

I use the search result and I GET
$s = $_GET['s'];
Which gives: "mario"
Now there is a category named Mario Bianchi under a main category called Persone
When I GET $s I need to get all posts within that category, I tried the following but I get nothing
$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . esc_html( $term->name ) . '</li>';
}
echo '</ul>';
}
Yet I need the actual post attached, not the category itself
get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
'taxonomy' => 'category',
'name__like' => $s,
'hide_empty' => true // Optional
) );
$term_ids = array();
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$term_ids[] = $term->term_id;
}
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_ids,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . esc_html( get_the_title() ) . '</li>';
}
echo '</ul>';
}
wp_reset_postdata();
}

WordPress custom taxonomy term repeats if have more than one post

I have links of term on sidebar When i attach more than one post to term it get repeat.
$args = new WP_Query(array('post_type' => 'song', 'post_status' => 'publish', 'posts_per_page' => '-1'));
while ($args->have_posts()) : $args->the_post();
$terms = get_the_terms( $post->ID, 'song-categories' );
if ($terms && ! is_wp_error($terms)){
foreach($terms as $term) {
if ($term_id == $term->term_id){ $curent_term = ' class="current"'; } else {$curent_term = '';}
echo '<li>'.$term->name.'</li>';
}
}
endwhile;
You aren't collecting all the terms here, you are collecting all the posts. 'posts_per_page' => '-1'
$args = new WP_Query(array('post_type' => 'song', 'post_status' => 'publish', 'posts_per_page' => '-1'));
while ($args->have_posts()) : $args->the_post();
So, for each post, you are getting all the terms:
$terms = get_the_terms( $post->ID, 'song-categories' );
You need to use a different method. In the past I have used things along the lines of:
function tax_list($tax, $current = null){
$terms = get_terms( $tax, 'orderby=count&hide_empty=1' );
foreach($terms as $term){
if($current == $term->term_id){
$class= " class='active'";
}else{
$class= "";
}
$name = $term->name;
$link = get_term_link( $term->slug, $tax );
echo "<li><a href='$link'$class>$name</a></li>";
}
}
Used as so:
tax_list($taxonomy, $term_id);
Reset Your Query before use of custom query
<?php wp_reset_query(); ?>

Categories