Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

post_orderby filter breaks function

$
0
0

Replies: 0

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 1 hour, 18 minutes ago by millertech.

Viewing all articles
Browse latest Browse all 59525

Trending Articles