Shortcode parameters not working
-
I have a page template “show-template.php”. This template will be applied across several pages. Each page the template is applied to will correspond to a season of a television show, i.e. “Season 1”, “Season 2”, etc.
I’m using the Advanced Custom Fields plugin to add custom fields to both posts and pages. The custom field name is “Season Number” and is referenced with the name “season_number”.
The “Season Number” field for the “Season 1” page is set to equal “1”. I have some posts, “Season 1 Episode #”, whose “Season Number” field is also set to equal “1”.
Using a single page template (“show-template.php”) I want ALM to pull in the corresponding posts. So, on the “Season 1” page, ALM will pull in all the Episodes which correspond to Season 1, but none of the episodes from other Seasons. On the Season 2 page, ALM will pull in all the Episodes from Season 2, but not from other seasons. Etc.
My repeater template is able to detect the “Season Number” value for each post, but I am not able to detect the “Season Number” value for the page itself.
In my “show-template.php” I have:
<?php $page_season_number = get_field('season_number'); ?>Now what I want to do is pass this $page_season_number variable into the shortcode as the meta_value. I have tried:
<?php echo do_shortcode('[ajax_load_more posts_per_page="1" button_label="View More" destroy_after="2" scroll="false" meta_key="season_number" meta_value="' . $page_season_number . '"]'); ?>However, the shortcode does not seem to work with meta_key and meta_value. I have tried hard-coding the meta_key to equal “season_number” and the meta_value to equal “2”, but ALM simply pulls in all posts regardless. I can’t seem to make the meta_value and meta_key work via shortcode.
I am able to pass the meta_value and meta_key into the get_post() method inside the repeater template. However, I have not successfully found a way to pass the $page_season_number value into the repeater template. I hope I have explained this clearly. I would be happy to clarify any point. Thank you for any help you can provide.
-
Hi.
Is there anyway you can test a standard WP_Query / Meta_Query to see if you can get the results using the values of the season?Do you know what I mean?
Hi dcooney, thanks for your speedy reply. I am somewhat familiar with the meta_query – or at least know where to find the documentation on it. How are you suggesting I use it? My apologies for not understanding fully – I have been working on this issue for many hours today and my brain is starting to get a little fried!
I’d be more than happy to try whatever you recommend. Thanks very much for your effort in troubleshooting, and for the excellent plugin.
I would like to add that during my testing of the ALM shortcode, I was not able to select by post_type either. I am able to get post_type to work when I pass it into the repeater template:
get_posts('post_type=post')Other ALM shortcode works fine, such as destroy_after, scroll, and button_label – but post_type, meta_key, and meta_value do not seem to work in my shortcode.
Hey, no problem.
I’m curious is the issue is actually related to the shortcode at all.
So I’m suggesting you put together a standard WordPress query using the parameters you are passing to the shortcode to ensure it actually works.For example.
$wp_query = new WP_Query (array ( 'post_type' => 'post', 'post_per_page' => -1, 'meta_query' => array( array( 'key' => 'season_number', 'value' => $page_season_number, 'compare' => 'IN' ) ) ));Excellent, thanks for your clarification. I will report back as quickly as possible.
Sorry for the late reply, I did not have access to my dev environment last night. I set up the WP_Query on the “show-template.php” page. I used the following:
<?php $wp_query = new WP_Query (array ( 'post_type' => 'mag_show_post', 'post_per_page' => -1, 'meta_query' => array( array( 'key' => 'season_number', 'value' => $page_season_number, 'compare' => '=' ) ) )); ?>The query returned the expected posts which match the season_number, so it seems to work well.
Ok thanks.
Is it possible to share a link to the page with the shortcode active?Unfortunately the page currently exists only in my local environment, and I believe client confidentiality may prohibit me from sharing even if it were not. I do realize this may make it very difficult or impossible to debug and apologize for the inconvenience. I would be happy to share any further details or code required.
Yea without a link I cant really debug.
But it sounds like an issue with the post type.Did you say you are unable to pass the post type?
I am unable to pass the post_type via the shortcode, but if I pass it into the get_posts() method in the repeater template it does work. The same is true for meta_key and meta_value.
This is the code I am using in the repeater template:
<span> <?php global $post; $recentposts = get_posts(array( 'post_type' => 'mag_show_post', 'meta_key' => 'season_number' )); foreach ($recentposts as $post) : setup_postdata($post); $post_season_number = get_field('season_number'); ?> //display each post here <?php endforeach; wp_reset_postdata(); unset($post); ?> </span>I’m not doing anything with $post_season_number in the repeater template because I wasn’t able to provide it access to the $page_season_number. This wouldn’t be a problem if I could pass $page_season_number through the shortcode, but unfortunately that’s been an issue as well. Somehow I need to provide both the $page_season_number and the $post_season_number in the same context so that I can compare the two.
I dont understand why you can’t access the post type in the shortcode.
What happens?
First I’ll note that I’m using the shortcode like so, not sure if it makes a difference:
<?php echo do_shortcode('[ajax_load_more posts_per_page="1" button_label="View More" destroy_after="2" scroll="false"]'); ?>All of the parameters you see there work perfectly. However, if I try this:
<?php echo do_shortcode('[ajax_load_more posts_per_page="1" button_label="View More" destroy_after="2" scroll="false" post_type="mag_show_post"]'); ?>or this:
<?php echo do_shortcode('[ajax_load_more posts_per_page="1" button_label="View More" destroy_after="2" scroll="false" meta_key="season_number" meta_value="2"]'); ?>Using either of those shortcodes, ALM will display only posts with the post_type of ‘post’. It will not display posts with the specified post_type or the specified meta_key.
!!!! I believe I’ve just solved my own problem, and I feel like a complete idiot!
I removed the get_posts method from my repeater template and the shortcode seems to be working beautifully now. I think I completely misunderstood how ALM works in regards to the repeater template. ALM is making its own calls to grab posts, I just need to provide it the specifics. Instead, I was trying to write the call to grab posts. Thanks so much for your assistance in this dcooney, I doubt I would have ever figured it out if I hadn’t been tinkering during this debug session. You rock!
yea, alm creates the query for you.
Best of luck!
The topic ‘Shortcode parameters not working’ is closed to new replies.