-1

hello i have this program (show posts with ajax paganition and ajax category filters) and its works fine what i want its add a pram that accept post type in shortcode because i need it to show posts from 2 diffrent post type movies and series. i've try a several way to do that but not work at all. hope you guys help me out.

my shortcode:

<?php echo do_shortcode('[ajax_filter_movies per_page="10"]'); ?>

which works fine but i want it be like :

<?php echo do_shortcode('[ajax_filter_movies post_type="movies" per_page="10"]'); ?>

basic codes from here.

0

Do you want to be able to switch between post types?

If not, seems to me you could just duplicate the same functionality as you're using with the per_page parameter, meaning:

In your shortcode function just add a data attr to the #container-async div for the post_type(s).

<div id="container-async" data-paged="<?php echo $a['per_page']; ?>" data-posttype="<?php echo $a['post_type']; ?>" class="sc-ajax-filter">

(you'll need to add it to your shortcode_atts as well by adding 'post_type' => '')

Then in your Javascript on('click') function add it to $params:

$params    = {
    'page' : $page,
    'tax'  : $this.data('filter'),
    'term' : $this.data('term'),
    'qty'  : $this.closest('#container-async').data('paged'),
    'pt'   : $this.closest('#container-async').data('posttype')
};

Finally, in your php function where you gather your $args array for WP_Query, get the value and then add in the post_type:

$pt = $_POST['params']['pt'];

$args = array(
    'paged'          => $page,
    'post_type'      => $pt,
    'post_status'    => 'publish',
    'posts_per_page' => $qty,
    'tax_query'      => $tax_qry
);
  • thanks for your time and answer sir i add your codes and call my shortcode like this : <?php echo do_shortcode('[ajax_filter_movies post_type="movies" per_page="10"]'); ?> but still its not work and show all posts from both post types. – Poorya Feb 23 '17 at 19:20
  • okay i add this sanitize_text_field() and now its work but the problem is its only show 1 shortcode each time i mean when i use 2 short code just 1 of them shows and the other show nothing,have no idea why its dont show 2 shortcodes at same time – Poorya Feb 23 '17 at 19:57
  • update your question to show me your whole shortcode function, the JS code, and the function hooked to the AJAX call. It's hard to envision what you mean when I can't see exactly what you're working with. – danbrellis Feb 23 '17 at 20:48
  • hey i update my codes every things works fine just dont know why 2 shortcodes dosent work at same time just 1 of them works and the other show's nothing at all. – Poorya Feb 24 '17 at 12:46
  • does the second shortcode output anything at all? Not even the HTML? If that's the case, I assume your if (count($terms)) returns false in the second shortcode and that's why nothing is output. Have you tried putting in a var_dump($terms) right before the if statement? – danbrellis Feb 24 '17 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.