• I have added a custom category to my pages: ‘sidekategori’

    I will be using this to group pages together and to show related pages.

    I’m using Elementor for layout and I’ve hit a wall trying to make a query that only includes pages with the same taxonomy ‘sidekategori’ as the current one.

    To use it as a filter in Elementor I have to create it as an action – so far so good. I’ve two other filters running that shows siblings and direct siblings, they’re working fine.

    But I can’t figure out how to filter by this custom category.

    This is my code:

    // Getting same sidekategori of current page
    add_action( 'elementor/query/pages_same_cpt_cat_filter', function( $query ) {
    	//get ID of current page
    	$id = get_queried_object_id();
    	// get current taxonomy
    	$tax = get_the_terms( $id, 'sidekategori');
    
    	$query->set( 'taxonomy', 'sidekategori');
    	$query->set( 'field', 'slug');
    	$query->set( 'slug', $tax[0]->slug );
    } );

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Thread Starter Morten Ellegaard Larsen

    (@ellegaarddk)

    Took me awhile to wrap my head around, but as always the answer came to me just as I asked the question. Or at least one way to do it:

    // Getting same sidekategori of current page
    add_action( 'elementor/query/pages_same_cpt_cat_filter', function( $query ) {
    	//get ID of current page
    	$id = get_queried_object_id();
    	// get current taxonomy
    	$tax = get_the_terms( $id, 'sidekategori');
    
    	$tax_query = array(
    		array(
    			'taxonomy' => 'sidekategori',
    			'field' => 'slug',
    			'terms' => $tax[0]->slug,
    		),
    	);
    
    	$query->set( 'tax_query', $tax_query );
    
    } );
Viewing 1 replies (of 1 total)
  • The topic ‘Query for getting pages within same category’ is closed to new replies.