Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What I'm working on is a ajax based dropdown option form which returns child terms based on the option selected.

When the option is selected, a function is fired using onchange, where the script grabs the term_id and taxonomy of the selected option, and sends both of those to the server.

The server side script I created accepts $_POST, I have verified that the variables are properly assigned by echoing them back to the client and checking the console logs.

<?php
//Include wordpress
require_once( 'wp-config.php' );
require_once( 'wp-includes/wp-db.php' );
$wpdb = new wpdb( DB_USER , DB_PASSWORD , DB_NAME , DB_HOST );

if (isset($_POST)){
    $taxID = $_POST[id];
    $taxType = $_POST[tx];
}
else {
    echo "Error: invalid data recieved. Please contact the site administrator.";
    die();
}

$termchildren = get_term_children( $taxID, $taxType );

echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxType );
    echo '<li><a href="' . get_term_link( $term->name, $taxType ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
die();
?>

The goal of this script is to return some html that I can insert into another element.
- The ID of the element causing issues is 4, which contains a child with ID of 15
- The taxonomy of the element is classifieds_categories
- Passing these values manually does not work either.
- When I echo the li a href I get this error:
Catchable fatal error: Object of class WP_Error could not be converted to string in ...

Does anyone have any suggestions? Because quite frankly I'm about to throw my laptop towards the nearest window at this point.

share|improve this question
    
I don't suppose you've var_dumped the WP_Error to see the contents? –  s_ha_dum Sep 7 '13 at 0:36
    
I've tried, however I can't seem to get further as PHP crashes before then. I am getting this from the log: link –  DWils Sep 7 '13 at 1:00

2 Answers 2

up vote 1 down vote accepted

Since get_term_link() will return a WP_Error object if the term does not exist, you could try:

$termchildren = get_term_children( $taxID, $taxType );

echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxType );

    $term_link = get_term_link( $term->name, $taxType );

    if( ! is_wp_error( $term_link ) )    
         echo '<li><a href="' . $term_link . '">' . $term->name . '</a></li>';

}
echo '</ul>';

so it looks like you are trying to echo the WP_Error object.

You could try to debug it with:

if( is_wp_error( $term_link ) )    
    echo $term_link->get_error_message();
share|improve this answer
    
Here's the edits based on your answer: link , which is returning: <ul>Empty Term taxType: classifieds_categories taxID: 4</ul>, I have the child Veterinarians with an ID of 15 that should be returned on this. –  DWils Sep 9 '13 at 17:11
1  
it might be helpful to check var_dump( $term ); inside the foreach loop. –  birgire Sep 10 '13 at 17:42
    
Thanks birgire for that info, this lead me to the solution. I fixed it by passing intval($term->term_id) as the first parameter in get_term_link (i passed it through intval because it doesnt seem to like non-integer variables) –  DWils Sep 11 '13 at 0:07
1  
great to hear you solved it and thanks for sharing your solution. –  birgire Sep 11 '13 at 1:05

Even though you are doing AJAX wrong, that isn't your problem. The problem is this line:

$wpdb = new wpdb( DB_USER , DB_PASSWORD , DB_NAME , DB_HOST );

But I don't see why you need either of these:

require_once( 'wp-includes/wp-db.php' );
$wpdb = new wpdb( DB_USER , DB_PASSWORD , DB_NAME , DB_HOST );

Try the code without those lines and it works, at least when I try.

But please do AJAX right.

share|improve this answer
    
Thanks for the resources, I will read them. However, removing those two lines of code does not seem to work. –  DWils Sep 7 '13 at 1:35
    
Post all of your code then –  s_ha_dum Sep 7 '13 at 1:55
    
Here's the part of the plugin responsible for displaying the data and including the js: link , here's the js: link –  DWils Sep 9 '13 at 17:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.