最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - unique post in "$args" of wp_query display more than one post

programmeradmin0浏览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();
}

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();
}
Share Improve this question asked Sep 4, 2019 at 10:16 Rak ArthusRak Arthus 116 bronze badges 2
  • Is the category a subcategory (or child category)? If so, see developer.wordpress/reference/functions/query_posts/… – Ted Stresen-Reuter Commented Sep 4, 2019 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 ? – Rak Arthus Commented Sep 4, 2019 at 10:40
Add a comment  | 

1 Answer 1

Reset to default 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).

发布评论

评论列表(0)

  1. 暂无评论