Show posts by slug

I am trying to retrieve specific posts by using slug description.

All posts (in this case products) are stored in “portfolio” where “automotive” is one of the
filters used as a category.

Read More

I have already seen some related posts on the web but due to my lack of PHP knowledge couldn’t figure it out so far and thought to give it a try here.

Here is the code used. Any help is appreciated, Thanks!

// Create a new `WP_Query()` object

$wpcust = new WP_Query(
    array(
        'post_type' => array('portfolio'),
        'tag_slug__in' => array('automotive'),
        'post__not_in' => array(1366, 1359, 1353),
    'orderby' => 'rand',
        'showposts' => '4' )
    );

Related posts

Leave a Reply

2 comments

  1. Here’s the parameters you can use in your WordPress Query: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

    • The post_type can be an array or a string.
    • The tag should be a
      string.
    • The post__not_in should be an array.
    • The orderby should
      be a string.
    • The showposts is DEPRECATED. You need to use
      posts_per_page instead. The posts_per_page should be an integer.

    posts_per_page (int) – number of post to show per page (available with Version 2.1, replaced showposts parameter). Use ‘posts_per_page’=>-1 to show all posts (the ‘offset’ parameter is ignored with a -1 value). Set the ‘paged’ parameter if pagination is off after using this parameter.

    Your code should be:

    $wpcust = new WP_Query(
        array(
            'post_type' => 'portfolio',
            'tag' => 'automotive',
            'post__not_in' => array(1366, 1359, 1353),
            'orderby' => 'rand',
            'posts_per_page' => '4' 
        )
    );
    
  2. May be this can help :-

    $wpcust = new WP_Query(
        array(
            'post_type' => 'portfolio',
            'tag_id' => '54',
            'post__not_in' => array(1366, 1359, 1353),
        'orderby' => 'rand',
            'posts_per_page' => '4' )
        );