• Hello,

    I have an events manager calendar with a lot events on a website. All events have event categories. Now I would like to display calendar data for some categories on another website (NOT a multisite).

    With an additional plugin on the second website, the ICS Calendar plugin, I managed to retrieve and display the .ics file from the events manager on the first site. Unfortunately, ALL events are shown.

    Now is there a way to make the .ics file only contain certain categories?

Viewing 7 replies - 1 through 7 (of 7 total)
  • You can restrict the ics file to only include certain categories using the following code snippet:

    add_filter('em_calendar_template_args',  function( $args ) {
    $args['category'] = [ 'category_1', 'category_2' ];
    return $args;
    });

    Replace the comma separated list of categories with the categories that you want to include. You can use either the category id or the category slug. If you put a minus sign in front of the category slug or id it will exlude that category.

    You can use the Code Snippets plugin to add the above code snippet.

    • This reply was modified 2 months, 3 weeks ago by joneiseman.
    Thread Starter cede007

    (@cede007)

    Thanks! Do I write this in the functions.php of the first page? So the page where the events manager runs? Or in another file?

    You can use the Code Snippets plugin to add the code snippet. Here are some other options: https://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/

    Thread Starter cede007

    (@cede007)

    Thank you very much for your help! If only one ics is needed, I will try your solution.

    But from the em calendar there will probably be several different ICS queries on other pages. If I understand correctly, I can only get a single .ics (either with all or individual categories), correct?

    I have now solved it using the CSS classes. All categories are hidden with display:none. And the category to be displayed is shown with display:block.

    Change the code snippet to this:

    add_filter('em_calendar_template_args',  function( $args ) {
    if (!empty($_GET) && is_array($_GET) && array_key_exists('category', $_GET)) {
    $category = sanitize_text_field($_GET['category']);
    $categories = explode(',', $category);
    $args['category'] = $categories;
    }
    return $args;
    });

    Then use a url like this:

    https://mysite.com/events/?ical=1&category=category_1,category_2

    This will allow you to use the query args to specify which categories you want to include in your ics file.

    You could extend this to filter by other event search parameters for example tags and scope:

    add_filter('em_calendar_template_args',  function( $args ) {
    if (!empty($_GET) && is_array($_GET)) {
    if (array_key_exists('category', $_GET)) {
    $category = sanitize_text_field($_GET['category']);
    $categories = explode(',', $category);
    $args['category'] = $categories;
    }
    if (array_key_exists('tag', $_GET)) {
    $tag = sanitize_text_field($_GET['tag']);
    $tags = explode(',', $tag);
    $args['tag'] = $tags;
    }
    if (array_key_exists('scope', $_GET)) {
    $scope = sanitize_text_field($_GET['scope']);
    $args['scope'] = $scope; // for example all,past,future,...
    }
    }
    return $args;
    });
    • This reply was modified 2 months, 3 weeks ago by joneiseman. Reason: correct the urll example
    • This reply was modified 2 months, 3 weeks ago by joneiseman.
    Thread Starter cede007

    (@cede007)

    Perfect, thank you very much! I’ll test your code as soon as it’s decided what exactly should be displayed on which page.

    One question: I could also write the code in the functions.php of the child theme, or would you not recommend that? The reason is that I want to use as few plugins as possible.

    Best regards!

    Yes, you can put the code snippet in the functions.php of your child theme.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.