0

I can't find the rights arguments to display only one post of a category when I click on a div. I call the post with Ajax and I display it in a div popup.Perhaps these arguments are not sufficient, or the problem come from another part?But when I echo the get_the_id in the loop it is the correct id of the post.However, it displays all posts in category. Another problem, if I don't specify a category (name or id) it display all posts of my Wordpress.These two problems come only when I call posts in Ajax.

function more_content() {

    $the_post_id = the_ID(); // $_POST['the_ID'];
    $args = array(
        'post_type' => 'post',
        'category_name' => 'materials',
        'p' => $the_post_id

    );

    $ajax_query = new WP_Query($args);
    $the_excerpt;
    $the_content;

    if ( $ajax_query->have_posts() ) : while ( $ajax_query->have_posts() ) : $ajax_query->the_post();

          $the_excerpt = the_excerpt();
          $the_content = the_content();

    endwhile;
    endif; 
    echo $the_excerpt;
  echo "<div style='color:white; font-size:40px;'>",$the_post_id,"</div>";
    echo $the_content;

    wp_reset_postdata();

    die();
}
2
  • Is the category a subcategory (or child category)? If so, see developer.wordpress.org/reference/functions/query_posts/… Sep 4 '19 at 10:21
  • it's a category (not a subcategory, not a child category). I think the main problem doesn't come with the category, even if it's not normal to define a category to display a unique post. the code above is the functions.php file. Do you think the problem could come from other file ex : ajaxcall.js ? Sep 4 '19 at 10:40
1

I am pretty sure that the Category parameters cannot be combined with p. In the documentation it says: "Show posts associated with certain categories." and none of the examples combine a category with a single post like you are trying to do.

Instead I think you need to use a taxonomy query as such:

$args = [
    'post_type' => 'post', 
    'p'=>(int)$_POST['the_ID'], 
    'tax_query' => [
        [
            'taxonomy'=>'category',
            'field'=>'slug',
            'terms'=>'materials'
        ]
    ]
];

Give that a try and remember to mark this as the answer if it solves your problem.

Also, be sure you are getting the right ID. In the code sample you provided, the_ID() is definitely not what you want. the_ID() echos the value of the ID to the output. What you probably want instead is get_the_ID() but even that will be wrong in this context (retrieving a single post by ID as defined by the input coming from the GET request).

And also, if you are plucking the ID from $_POST or $_GET, be sure to sanitize it prior to using it in your query (although, I'm pretty sure WordPress also sanitizes input prior to running the query, you can never be too safe).

5
  • The result is the same. I have try with "get_post" (to get my post without a loop) and with global "Wp_query" but i'm not sure to write the correct code. I handed the right code for the ID $_POST['the_ID']; // the_ID(); and I have add this code in my loop : echo "<p class='col-white font-size-2rem font-weight-bolder'>",$the_post_id,"<p>"; that display me the id corresponding of each post . Currently I have two post for the test. When I call in ajax my post for popup clicking in list one of my two posts the link of post displaying in the browser is the right id of this post. Sep 4 '19 at 16:42
  • I noticed that my callajax.js seems to be wrong because I can't get the id of the post with an "alert" and I get "undefined" result. my jquery code : Sep 4 '19 at 16:44
  • jQuery(document).ready(function(){ jQuery('.post-link').click(function() { var post_id = jQuery(this).data('id'); jQuery.post( ajaxurl, { 'action': 'more_content', 'the_ID': post_id }, function(response){ // on affiche la réponse ou l'on veut jQuery('#more-content').html(response); } ); alert (post_id); return false; }); jQuery(".close-more").click(function(){ jQuery('#more-content').html(''); }); }); Sep 4 '19 at 16:44
  • I invite you to check out wp-cli.org The tool includes a WordPress "shell" that allows you to test code just by typing it into a terminal. Using that tool, you can easily test your code until you are happy with how it works. I would start implementing the code by making it work outside of ajax first, and then, following the principles of Progressive Enhancement, make it work through ajax. Without a complete code example it is very difficult for us to provide a concrete answer. Sep 5 '19 at 16:02
  • The answer of my question : Hi. my problem is resolved. I have replaced in callajax.js jQuery(this).data('id'); by jQuery(this).attr('id'); and add inside html page : id=",the_ID()," . Also I removed : 'category_name' => 'materials', thanks for your help Sep 5 '19 at 16:27

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.