• Hi there,

    I’m a complete beginner at coding and need some help please.
    I’m trying to get a list of EDD products by a specified category and have ended up with this:

    <?php
    query_posts(array(
    ‘post_type’ => ‘download’,
    ‘cat’ => ’46’,
    ‘showposts’ => 100
    ) );
    ?>
    <?php while (have_posts()) : the_post(); ?>
    <h2>“><?php the_title(); ?></h2>

    <?php endwhile;?>
    (This sits on it’s own template page ).

    This returns a blank page.
    However, ‘cat’ => ‘ ‘ returns a list of every product title as hyperlinks (I’ve tried this with WP_Query with the same results).
    I’m confused, how do I specify the category please?

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 40 total)
  • Shaylee

    (@shayleehansen30)

    Have you checked out the EDD documentation? This page http://docs.easydigitaldownloads.com/article/224-downloads looks like it might be helpful.

    Thread Starter ageingdj

    (@ageingdj)

    Hi Shaylee,
    Thanks for your quick reply.
    I’ve already tried that! but I’m trying this by coding
    I’ve also tried getting the same listing from posts and pages by the same methods and with the same results –
    cat=>anynumber returns no results
    cat=>’ ‘ returns a complete list
    and I can’t figure this out!

    Shaylee

    (@shayleehansen30)

    Thread Starter ageingdj

    (@ageingdj)

    Hi Shaylee,
    Thanks for taking the time.
    This was the third method I tried (code below).
    I was trying to follow a YouTube tutorial (https://www.youtube.com/watch?v=rw3jRE8IslM) and hit a brick wall.

    <?php /* Template Name: CodePageTemplate */ ?>
    //Category loop One starts here
    <?php
    $opinionPosts = new WP_Query(‘cat=>46’);
    query_posts(‘post_type=edd_download’);
    if ($opinionPosts->have_posts()) :
    while ($opinionPosts->have_posts()): $opinionPosts->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile;
    else :
    //Here
    endif;
    ?>

    //Category loop Two starts here
    <?php
    $querypaul = Array(‘cat=>46 ‘);
    $opinionPosts = new WP_Query( $querypaul );
    if ($opinionPosts->have_posts()) :
    while ($opinionPosts->have_posts()): $opinionPosts->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile;
    else :
    //Here
    endif;
    ?>

    //Category loop Three starts here
    <?php
    query_posts(array(
    ‘post_type’ => ‘download’,
    ‘cat’ => ’46’,
    ‘showposts’ => 100
    ) );
    ?>
    <?php while (have_posts()) : the_post(); ?>
    <h2>“><?php the_title(); ?></h2>
    <p><?php echo get_the_excerpt(); ?></p>
    <?php endwhile;?>

    Thread Starter ageingdj

    (@ageingdj)

    Sorry the download cat should be 112 (still don’t work though).

    Shaylee

    (@shayleehansen30)

    I checked out the codex and under Querying by Post Type might be helpful https://codex.wordpress.org/Post_Types#Querying_by_Post_Type

    According to the docs you don’t even need to use WP_Query you should just be able to declare the array ‘edd_download’ post type like in the docs.

    In that first block of code I noticed you had:
    query_posts(‘post_type=edd_download’);

    <?php /* Template Name: CodePageTemplate */ ?>
    //Category loop One starts here
    <?php
    $opinionPosts = new WP_Query(‘cat=>46’);
    query_posts(‘post_type=edd_download’);
    if ($opinionPosts->have_posts()) :
    while ($opinionPosts->have_posts()): $opinionPosts->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile;
    else :
    //Here
    endif;
    wp_reset_postdata(); <!–Include that –>
    ?>

    • This reply was modified 6 years, 3 months ago by Shaylee.
    Shaylee

    (@shayleehansen30)

    So I think it should look like:
    <?php /* Template Name: CodePageTemplate */ ?>
    //Category loop One starts here
    <?php
    $args = array( ‘post_type’ => ‘edd_download‘, ‘cat’ => 46 , ‘posts_per_page’ => 10);
    $loop = new WP_Query( $args );

    <!— loop code –>

    ?>

    • This reply was modified 6 years, 3 months ago by Shaylee.
    • This reply was modified 6 years, 3 months ago by Shaylee.
    Thread Starter ageingdj

    (@ageingdj)

    Sorry Shaylee, still no luck.
    Code sets one,two & four all give ten posts (not dowloads)
    Code set three gives the same list of all downloads
    I’m packing up for the night!
    Thanks for all your help

    <?php /* Template Name: CodePageTemplate */ ?>
    //Category loop One starts here
    <?php
    $opinionPosts = new WP_Query(‘cat= ‘);
    query_posts(‘post_type=download’);
    if ($opinionPosts->have_posts()) :
    while ($opinionPosts->have_posts()): $opinionPosts->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile;
    else :
    //Here
    endif;
    wp_reset_postdata();
    ?>

    //Category loop Two starts here
    <?php
    $querypaul = Array(‘cat=> ‘);
    $opinionPosts = new WP_Query( $querypaul );
    if ($opinionPosts->have_posts()) :
    while ($opinionPosts->have_posts()): $opinionPosts->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile;
    else :
    //Here
    endif;

    ?>

    //Category loop Three starts here
    <?php
    query_posts(array(
    ‘post_type’ => ‘download’,
    ‘cat’=> ”,
    ‘showposts’ => 100
    ) );
    ?>
    <?php while (have_posts()) : the_post(); ?>
    <h2>“><?php the_title(); ?></h2>
    <p><?php echo get_the_excerpt(); ?></p>
    <?php endwhile;
    wp_reset_postdata();
    ?>

    //Category loop Four starts here
    <?php
    $args = array( ‘post_type’ => ‘download’, ‘cat’ => 112 , ‘showposts’ => 10);
    $loop = new WP_Query( $args );

    if ($opinionPosts->have_posts()) :
    while ($opinionPosts->have_posts()): $opinionPosts->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile;
    else :
    //Here
    endif;
    wp_reset_postdata();
    ?>

    Moderator bcworkz

    (@bcworkz)

    Loop four is probably closest to being correct, but there are a few issues to correct. Instead of ‘showposts’, use ‘posts_per_page’. Showposts has been deprecated for quite some time. Replace all occurrences of $opinionPosts with $loop. The new WP_Query object variable has to be the one driving the loop.

    According to Shaylee, the proper post type is 'edd_download', not just 'download'. I’m not familiar with the plugin so couldn’t say one way or another, but I’m inclined to believe Shaylee is correct. It’s always a good idea to prefix name slugs with unique initials to prevent name collisions. “download” would have been a poor choice for a slug name, as would any plain English words.

    Both you and Shaylee ought to be demarcating all code posted in these forums with `backticks`, or using the “code” button after highlighting the code. When you do not do this, the code becomes corrupted and unusable without further editing. No one can easily test your code or post working corrections that could be copy/pasted. Help us help you 🙂

    Thread Starter ageingdj

    (@ageingdj)

    Hi Bcworkz,
    Thanks for your reply.
    I’ve ended up with the code below,
    I’ve tried this with post_type=> page/post/portfolio and download (edd_download gives a blank page), and using tag instead of cat, with the same results:
    ‘cat’=> ‘ ‘ returns a list of all downloads(or pages, posts etc)
    ‘cat’=> ‘anynumber‘ returns a blank page
    Beats me
    Paul

    //Category loop Four starts here
    <?php
    $args = array( 'post_type' => 'download', 'cat' => ' '  , 'posts_per_page' => -1);
    $loop = new WP_Query( $args );
    
    if ($loop->have_posts()) :
    while ($loop->have_posts()): $loop->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile;
    else : 
    //Here
    endif;
    wp_reset_postdata();
    ?>

    (Apologies, I’m sure I’m not the only person who didn’t know about highlighting code)

    Moderator bcworkz

    (@bcworkz)

    The cat argument is only effective when an integer is supplied as a value. A numeric string (using quotes) is invalid. Use 'cat'=>112 not 'cat'=>'123' PHP is supposed to be loosely typed, but it’s sometimes finicky about data type.

    I’m sorry I couldn’t tell you the correct post type name slug. It’s not simple to reliably discover the correct slug. One way is to find the code where the post type is registered. Another is to look at a post listing of that type in the posts DB table by using phpMyAdmin. Or get a known post of the type by ID and var_dump() the returned object. The slug that appears in a single product’s permalink might be the correct slug, but it could also be altered, so permalink is not a reliable indicator.

    If you need to specify more than one post type, it should be in array format. You end up with an indexed array within an associative array. For example:

    $args = array(
       'post_type' => array(
          'post',
          'page',
          'portfolio',
       ),
       'cat' => 112, 
       'posts_per_page' => -1,
    );

    You don’t have to use a multi-line assignment like that, but it’s generally easier to read IMO.

    (Apologies, I’m sure I’m not the only person who didn’t know about highlighting code)

    No worries, I only mentioned it FYI. You are soooo right on this one! There used to be a note about it below the new topic form, but it was removed in an effort to clean up the form ¯\_(ツ)_/¯

    Thread Starter ageingdj

    (@ageingdj)

    Hi,
    I’m confused!
    Your code:

    $args = array( 'post_type' => array(
        'post',
        'page',
        'portfolio',
        'download',),   <<<<<added line
           'cat' => 112  , 'posts_per_page' => -1,);
    $loop = new WP_Query( $args );

    Returns a blank page
    However, moving the “),”

    $args = array( 'post_type' => array(
        'post',
        'page',
        'portfolio',
        'download',
           'cat' => 112  , 'posts_per_page' => -1,),); 

    Returns what looks like a random selection of file names (including Process.php) – and it seems to ignore the last line.
    Weird, maybe it’s my theme.

    Moderator bcworkz

    (@bcworkz)

    No, don’t move the closing inner ‘)’. It must only demarcate post types. There’s something else wrong somewhere. It’s probably a simple syntax error. Edit your wp-config.php in the WP installation’s root folder. There’s a line sort of near the bottom defining WP_DEBUG as false. Change it to true: define('WP_DEBUG', true);

    Reload the problem page. You should now see an error message with what and where there’s an error. Sometimes the error is actually above the named line number. The line reference is where the parser realized there’s an error, it’s not always the exact location. Sometimes the error description doesn’t make sense. Often Googling the error description will turn up examples of the error.

    Another good thing to do is output a message when no posts are found. Between else : and endif;, change the //Here to something like echo 'Query failed to find posts matching the criteria';

    Not knowing for sure the proper post type name slug is bugging me. Let’s try something to find out for sure. Go to the back end post list screen for EDD products. Follow the edit link for any product. In the browser address bar for the edit screen, note the integer following “post=” in the URL. Place this integer as the get_post() argument in the following code. Place this code before or after the category loop code we are working on. Be sure it’s still within the <?php ?> delimiters.

    $product = get_post( 123 ); //replace 123 with an actual product post ID
    echo "Proper product slug name: {$product->post_type}<br>\n";

    This will tell us what we need to know once other page errors are resolved.

    Thread Starter ageingdj

    (@ageingdj)

    Hi there,
    Thanks for all your trouble
    I’ve changed the line in the wp-config.php file and got a few messages but nothing to do with this query
    (“Notice: wp_register_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.) in /home/k21ageingdj/public_html/wp-includes/functions.php on line 4146.”)
    I’ve also made the other changes you suggested which gave a result of:

    //Category loop Five starts here
    Query failed to find posts matching the criteria
    Proper product slug name: download

    The code I ended up with is:

    //Category loop Five starts here<br>
    <?php
    $args = array( 'post_type' => array(
        'post',
        'page',
        'portfolio',
        'download'),
           'cat' => '112', 'posts_per_page' => 10,);
    $loop = new WP_Query( $args );
    
    if ($loop->have_posts()) :
    while ($loop->have_posts()): $loop->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile;
    else : 
    echo 'Query failed to find posts matching the criteria <br>';
    endif;
    wp_reset_postdata();
    $product = get_post( 4290 ); //replace 123 with an actual product post ID
    echo "Proper product slug name: {$product->post_type}<br>\n";
    ?>
    Moderator bcworkz

    (@bcworkz)

    OK, “download” it is! Sorry to doubt you, it was just too simple for comfort 🙂 Thanks for playing along.

    We’re getting oh so close! It all looks good except for one very important thing. Please remove the quotes from the 112 of the ‘cat’ argument: 'cat'=> 112,

    That notice should be resolved at some point, but you’re right, it’s unrelated.

Viewing 15 replies - 1 through 15 (of 40 total)
  • The topic ‘How to get “cat=>” working’ is closed to new replies.