• Resolved Josh Robbs

    (@jwrobbs)


    I created a basic shortcode. It takes attributes like count, offset, and category. It outputs a list of posts based on that info.

    The issue is that it doesn’t work if there is an apostrophe in the category name. How do I make this work? I don’t even know what I’m looking for.

    Example shortcode
    [articles cat="Doesn't work"]

    Function:
    ***
    Note 1: I’m not showing the functions that set the other variables.
    Note 2: This works for other categories – single or common separated.
    ***

    function output($atts) {
        $queryTerms = [];
        $queryCats = explode(",",$atts['cats']);
        foreach ($queryCats as $queryCat) {
            $queryTerms[] = $queryCat;
        }
       
        $taxQuery = array (
            'relation' => 'OR',
    	    array (				
    		'taxonomy' => 'article_category',
    		'field' => 'name',
    		'terms' => $queryTerms,
    		'operator' => 'IN',
    					
    	));
        $query = new WP_Query( array(
    	'post_type'	=> $postQuery,
            'posts_per_page' => $postCount,
            'order' => 'DESC',
            'orderby' => 'date',
    	'offset' => $offset,
    	'tax_query' => $taxQuery,
        ));
    
        if ( $query->have_posts()) :
            while ( $query->have_posts() ) : $query->the_post();
    
        //etc etc
    }
    • This topic was modified 6 years, 2 months ago by Josh Robbs.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How do you query by category by name with apostrophe?’ is closed to new replies.