Elementor: How To Display Custom Posts Using a Matching Taxonomy and Post Title

Smart Web Creative - Elementor: How To Display Custom Posts Using a Matching Taxonomy and Post Title

Have you ever tried to display posts from one custom post type on a page template for another custom post type using Elementor? Well, there is one way you can accomplish and that is to display custom posts using a matching taxonomy and post title.

As an example, let’s say you have a custom post type called, Instructor Videos and another custom post type called, Instructors. You want to display videos of a certain instructor dynamically on a page template of “Instructors”. To make this work, you will first need to create a parent taxonomy/category for Instructor Videos. In this case, let’s call it “All Videos”. Then you will need to assign each video a taxonomy/category name (with “All Videos” as the parent) and base it on the name of the instructor. For instance, you may have videos from an instructor named, “John Smith”. Categorize those videos of him with the taxonomy name, “John Smith”.

Now, the second part is that your instructor post title must be the exact same as the name of the taxonomy. So, when you create your custom post within Instructors of the instructor, “John Smith”, “John Smith” is the taxonomy name you create within your Instructor Videos.

The next step is adding some code to your theme’s function.php file. You can copy and paste the code below:


/* Display Custom Posts Using a Matching Taxonomy and Post Title */
function custom_query_callback( $query ) {

//get ID of current page
$id = get_queried_object_id();
// get current taxonomy
$title = get_the_title();

$tax_query = array(
array(
'taxonomy' => 'taxonomy_slug',
'field' => 'name',
'terms' => $title,
),
);

$query->set( 'tax_query', $tax_query );
}
add_action( 'elementor/query/get_taxonomy_name', 'custom_query_callback' );

You will need to change some things to make it work for your site.

You need to change “taxonomy_slug” to the actual taxonomy slug for the taxonomy you created. In our example, “instructor_videos” should be the taxonomy slug of Instructor Videos.

Next, you’ll want to change, “get_taxonomy_name” to the actual name of the taxonomy you assigned. In our example, “all_videos” should be the taxonomy name which is the parent taxonomy/category. However, you will want to keep the “get_” part. So it should look like, “get_all_videos”.

Other Helpful Articles

Tips For Setting Up An E-Commerce Store

Need some tips for setting up an e-Commerce store? Having an online shop, such as an e-Commerce store, can be both exciting and overwhelming. However, if you market it right, you can make a good income off of it. When web design companies build e-Commerce sites for their clients, there’s

Read More »

Online Generator for Writing Terms & Conditions

Many websites need a terms and conditions page. A “Terms & Conditions” page usually refers to guidelines that website users should follow while using the site. Creating your own terms and conditions from scratch can get tricky and become time consuming. To make things easier, there are some generators online

Read More »
Smart Web Creative - Elementor: Create Conditional Form Fields with Code

Elementor: Create Conditional Form Fields with Code

If you ever wanted to create conditional form fields with code using Elementor Pro, this tutorial will guide you on how to do that. We will be using an example where users will be asked if they would like to enter their address details. It will look like this once

Read More »

What Do You Need Done?:

(Please check all that apply)

Tell Us About You

Almost Done!

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info