Products
GG网络技术分享 2025-03-18 16:12 23
I have three posts, two which have the tag eGuide
one which has the tag Article
. When I click on an eGuide
, I want the sidebar to display other eGuide\'s (up to 2) and nothing else. To do this, I have the following query:
global $post;$args = array(
\'post_type\' => \'resources\',
\'category__in\' => wp_get_post_categories($post->ID ),
\'posts_per_page\' => 3,
\'post__not_in\' => array($post->ID )
);
$relatedPosts = new WP_Query( $args );
But it\'s showing the article
too? I\'ve also tried:
\'post__not_in\' => array(get_the_ID() )
... still no luck.
global $post;$term_list = wp_get_post_terms( $post->ID, \'your_taxonomy_here\', array( \\\"fields\\\" => \\\"ids\\\", \\\"parent\\\" => 0 ) );
$args = array (
\'post_type\' => \'resources\',
\'posts_per_page\' => 3,
\'post__not_in\' => array( $post->ID ),
\'post_status\' => \'publish\',
\'tax_query\' => array(
array (
\'taxonomy\' => \'your_taxonomy_here\',
\'field\' => \'term_id\',
\'terms\' => $term_list[0],
),
),
);
$relatedPosts = new WP_Query( $args );
You can try this code to get related posts.
Demand feedback