Title: post_orderby filter breaks function
Last modified: April 10, 2022

---

# post_orderby filter breaks function

 *  [millertech](https://wordpress.org/support/users/millertech/)
 * (@millertech)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/post_orderby-filter-breaks-function/)
 * I have a custom shortcode function to display a list of articles. The list needs
   independent styling based on the post tag. That part all works. I need it to 
   be sorted by term_order. I created a post_orderby filter to override the query,
   but when I apply it, it breaks the link to the page. Any help on why this might
   be happening is appreciated.
 *     ```
       //add filter to change the order by of the query done by WP_QUERY:
       function my_query_orderby($orderby) {
       	global $wpdb;
       	return "{$wpdb->term_relationships}.term_order ASC";
       }
       add_filter('posts_orderby', 'my_query_orderby');
   
       function create_article_list($attr) {
       	// Initialize variable and check for shortcode content
       	$return = '';
       	if( $content ) 
       	{
       		$return = $content;
       	}
       	$args = shortcode_atts( array(
       		'term' => '68',
       	), $attr );
   
       	$terms = array($args['term']);
   
       	$query = new WP_Query( array(
       		'post_status' => 'publish', 
       		'numberposts' => -1,
       		'post_type' => 'user_guide',
       		'orderby' => 'term_order',
       		'order' => 'ASC', 
       		'tax_query' => array(
       			array(
       				'taxonomy' => 'user_guide_categories',
       				'terms' => $terms,
       				'field' => 'term_id',
       				'operator' => 'IN',
       				'include_children' => true
       			)
       		), 
       	) );
       	while ( $query->have_posts() ) 
       	{
       		$first = '';
       		$tags = array();
       		$tags = get_the_tags();
       		$tag = $tags[0]->name;
       		if ($tag == "essential") 
       		{
       			$return .= 
       				$first
       				. '<a id=article-list-user-guide href="' . get_permalink() . '">'
       				. '<i class="fa-solid fa-star-of-life"></i>'
       				. get_the_title() 
       				. '</a><br>';
       			$first = '<br>';
   
       		} elseif ($tag == "admin") 
       		{
       			$return .= 
       				$first
       				. '<a id=article-list-admin-guide href="' . get_permalink() . '">'
       				. get_the_title() 
       				. '</a><br>';
       			$first = '<br>';
       		}
       	}
       	return $return; 
       }
       add_shortcode('my_shortcode', 'create_article_list');
       ```
   
    -  This topic was modified 4 years, 1 month ago by [millertech](https://wordpress.org/support/users/millertech/).
    -  This topic was modified 4 years, 1 month ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).
      Reason: Moved to Fixing WordPress, this is not an Everything else WordPress
      topic

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/post_orderby-filter-breaks-function/#post-15544894)
 * “term_order” does not appear to be a valid orderby arg. List of valid args in
   source:
    [https://core.trac.wordpress.org/browser/tags/5.9/src/wp-includes/class-wp-query.php#L1641](https://core.trac.wordpress.org/browser/tags/5.9/src/wp-includes/class-wp-query.php#L1641)
 * AFAIK ordering by a term’s name requires a custom SQL query. You can replace 
   the SQL WP comes up with with one that orders by term name through the “posts_request”
   filter.
 *  Thread Starter [millertech](https://wordpress.org/support/users/millertech/)
 * (@millertech)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/post_orderby-filter-breaks-function/#post-15545112)
 * Thank you [@bcworkz](https://wordpress.org/support/users/bcworkz/) – those are
   the included options with WP_Query and the filter should override those. I’ve
   tried different SQL & WP methods within the filter, but the issue is that the
   filter itself seems to be breaking the link to the page no matter what the filter
   content is. I will take a look the the post_request filter.
    -  This reply was modified 4 years, 1 month ago by [millertech](https://wordpress.org/support/users/millertech/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘post_orderby filter breaks function’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [millertech](https://wordpress.org/support/users/millertech/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/post_orderby-filter-breaks-function/#post-15545112)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
