WordPress.org

Ready to get started?Download WordPress

Forums

code error on an implode function (11 posts)

  1. csleh
    Member
    Posted 1 year ago #

    I'm using code on this page:
    http://wordpress.org/support/topic/sorting-by-post-type-in-a-tag-archive-archivephp?replies=5

    which works, but is putting an error at the top of the web page:
    "Warning: implode() [function.implode]: Invalid arguments passed in /home/csleh3/public_html/sandbox/wp-content/themes/aodmarketing/functions.php on line 279"

    which is this part of the solution:

    add_filter( 'posts_orderby', 'sort_query_by_post_type', 10, 2 );
    function sort_query_by_post_type( $sortby, $thequery ) {
    	if(is_tag() && empty( $query->query_vars['suppress_filters'] ) && !is_admin()) {
    		$thequery->set('orderby','post_type');
    		if ( !empty($thequery->query_vars['post_type']) && isset($thequery->query_vars['orderby']) && $thequery->query_vars['orderby'] == 'post_type' )
    			$sortby = "find_in_set(post_type, '" . implode( ',', $thequery->query_vars['post_type'] ) . "')";
    	}
    	return $sortby;
    }

    any idea on what's wrong with the implode line?

  2. s_ha_dum
    Member
    Posted 1 year ago #

    $thequery->query_vars['post_type'] is not an array. That is what the "Invalid arguments" part means. I'd guess that it is a string, at least sometimes. What does var_dump($thequery->query_vars['post_type']); tell you?

  3. csleh
    Member
    Posted 1 year ago #

    Adding var_dump($thequery->query_vars['post_type']); showed "NULL"

    I had left off the beginning. here is the entire function:

    function tc_tag_for_cpt($query) {
      if(is_tag() && empty( $query->query_vars['suppress_filters'] ) && !is_admin()) {
    	$post_type = get_query_var('post_type');
    	$post_type = ($post_type) ?  $post_type : array('resources','post','page'); //,'nav_menu_item'
        $query->set('post_type',$post_type);
    	return $query;
      }
    }
    add_filter( 'posts_orderby', 'sort_query_by_post_type', 10, 2 );
    function sort_query_by_post_type( $sortby, $thequery ) {
    	if(is_tag() && empty( $query->query_vars['suppress_filters'] ) && !is_admin()) {
    		$thequery->set('orderby','post_type');
    		if ( !empty($thequery->query_vars['post_type']) && isset($thequery->query_vars['orderby']) && $thequery->query_vars['orderby'] == 'post_type' )
    			$sortby = "find_in_set(post_type, '" . implode( ',', $thequery->query_vars['post_type'] ) . "')";
    	}
    	return $sortby;
    }
  4. s_ha_dum
    Member
    Posted 1 year ago #

    You posted not "the entire function", but two functions and a filter, and I don't see the first function used anywhere. Perhaps you've left something out? That first first function would set 'post_type' to an array under some circumstances, though not all.

  5. csleh
    Member
    Posted 1 year ago #

    The goal is to sort tag archive by kind: post, page, or custom post type, instead of by creation date. It actually works -- adding this code the list is sorted by type. But there is an error message at the top of the page.

    The code is in functions.php.

    It looks to me like the first bit sets up "suppress_filters" query and tells wordpress what type of post to look for (post, page, or custom post type "resources")

    Line 279 wordpress complains about is the implode one. I don't know what the invalid argument is in that line or section.

  6. s_ha_dum
    Member
    Posted 1 year ago #

    It mostly works, not "It actually works". It should not work as intended if 'post_type' isn't being set correctly.

    Line 279 wordpress complains about is the implode one. I don't know what the invalid argument is in that line or section.

    I told you the answer to this. "$thequery->query_vars['post_type'] is not an array", which you confirmed by telling me that "[a]dding var_dump($thequery->query_vars['post_type']); showed "NULL"". NULL is not an array, and implode takes an array as a parameter. The questions are "why is it NULL?" and "Why does it get past if ( !empty($thequery->query_vars['post_type']) given that it is NULL?"

    Where did you put that var_dump?

  7. csleh
    Member
    Posted 1 year ago #

    AH I thought it was pulling post_type from $post_type above, which had an array. I don't understand much evidently.

    I put the var_dump after the above code in functions.php and refreshed the page.

  8. s_ha_dum
    Member
    Posted 1 year ago #

    AH I thought it was pulling post_type from $post_type above, which had an array. I don't understand much evidently.

    No. Like I said, that first function isn't being used. It is defined, but never called. It won't execute.

    I put the var_dump after the above code in functions.php and refreshed the page.

    Yes, but where?. It could potentially be different in each of those two functions and outside of it.

  9. csleh
    Member
    Posted 1 year ago #

    var_dump($thequery->query_vars['post_type']);
    - outside and after both returns "null"
    - inside the function tc_tag_for_cpt returns nothing

    - before add_filter returns "null"

    - inside the add_filter before } return $sortby; }
    returns "string(3) "any""

  10. s_ha_dum
    Member
    Posted 1 year ago #

    tc_tag_for_cpt() returns nothing because it isn't executing.

    By "add_filter", I assume you mean "sort_query_by_post_type", and that confirms my original guess. It is a string that is causing the trouble.

    It looks like tc_tag_for_cpt() should be called or hooked from somewhere but I'll have trace that down. I don't know if or when I'll have time to get to is.

  11. csleh
    Member
    Posted 1 year ago #

    can anyone else help me out with this? I'm trying to get the results page to show posts with the same tag, but sorted by post type (including custom post type) instead of date.

Topic Closed

This topic has been closed to new replies.

About this Topic