You have lot of issues here;
-
Never ever use
query_posts
, it breaks the main query and page functionalities. -
You are breaking and nullifying the main query even before
query_posts
can have a change to do it in this line$wp_query= null
-
As this is a shortcode (taken from your comments) and with this line,
'posts_per_page' => $num,
, I believe that you are usingextract()
. You should never useextract()
as it is almost un-debuggable, unreliable and fails silently. For this reason it was removed from core functions` -
The correct value to
orderby
to sort by post date isdate
, notpost_date
. This is the default, so you can just omit this as you are not modifying it through an attribute. This also goes foroffset
andorder
-
You need to use a
tax_query
to get posts from a specific term
Here is a small example you can work from. (Please note, you should sanitize and validate all user inputs. I have not done any of that).
add_shortcode( 'my_shortcode', function ( $atts )
{
$attributes = shortcode_atts(
[
'numberposts' => 10, // Default amount of posts to show
'post_type' => 'technologies', // Default post type
'taxonomy' => 'importance', // Default taxonomy
'terms' => 'featured' // Default term to exclude
],
$atts,
'my_shortcode'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'posts_per_page' => $attributes['numberposts'],
'post_type' => $attributes['post_type'],
'tax_query' => [
[
'taxonomy' => $attributes['taxonomy'],
'field' => 'slug',
'terms' => $attributes['terms'],
'operator' => 'NOT IN' // This will exclude all posts with terms set in 'terms'
]
],
'post_status' => 'publish',
'paged' => $paged,
'suppress_filters' => true
];
$q = new WP_Query( $args );
$output="";
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
$output .= apply_filters( 'the_title', get_the_title() );
}
$output .= get_next_posts_links( 'Next', $q->max_num_pages );
$output .= get_previous_posts_links( 'Previous' );
wp_reset_postdata();
}
return $output;
});
You can just call it as [my_shortcode]
if you don’t need to change the defaults
FINAL NOTES
-
I assumed that you would want to exclude the
featured
term, so I have written thetax_query
around that. -
You will need PHP5.4+ as the code uses the new short array syntax (
[]
). If you have an older version, just change to the old array syntax (array()
). Just be sure then you have at least PHP 5.3 to support the closure used