How to get “cat=>” working
-
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]
-
Hi there,
‘cat’ => 112, returns ‘Query failed to find posts matching the criteria’
cat => ‘112’, returns ‘Query failed to find posts matching the criteria’
‘cat’ => ‘ ‘, returns random (I think) downloads
‘category’ => ‘AllyPally’ returns random downloads
‘category_name’ => ‘AllyPally’ returns ‘Query failed to find posts matching the criteria’ [AllyPally is the name of cat 112].
It makes no sense!
Many thanks
Paul//Query Five starts here<br> <?php $args = array( 'post_type' => array( 'download'), 'category_name' => 'AllyPally', 'posts_per_page' => 15,); $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(); ?>
Well, quotes are definitely incorrect for integers and required for alphabetic values. At this point, the version of your code following those rules is correct and the reason for the query not returning anything lies elsewhere. FYI, when you only have one post_type value, you do not need an array structure, a simple string will suffice. However, it’s completely valid to pass a one element array as well, so no problem there.
A few more FYI comments — there is no valid ‘category’ argument in WP_Query. ‘category_name’ is valid but misleading because it wants the category slug, not name. Like post slugs, category slugs should be all lowercase with only hyphens allowed to divide words, no spaces. Slugs do not have to match the name but usually do. As a guess, the category name argument should have been something like
'category_name' => 'allypally',
Because most web servers run some variant of Linux, and Linux is case sensitive'allypally' != 'AllyPally'
Anyway, to get an idea why nothing is returned, let’s look at the SQL query that WP_Query runs based on your arguments. Insert the following line right below the new WP_Query line and above the if have posts line:
echo "Query: {$loop->request}<br>\n";
There are a number of filters that plugins and themes can use to modify any query. Sometimes poorly written plugins or themes will erroneously modify queries they shouldn’t. How the query is built could give us a clue to how to solve this puzzle.
Hi there,
I haven’t used SQL for years!
This is the query your code returned:$args = array( 'post_type' => array( 'download'), 'cat' => 112, 'posts_per_page' => 15,); $loop = new WP_Query( $args ); echo "Query: {$loop->request}<br>\n";
Query: SELECT SQL_CALC_FOUND_ROWS wpot_posts.ID FROM wpot_posts
WHERE 1=1 AND ( 0 = 1 ) AND wpot_posts.post_type = ‘download’ AND (wpot_posts.post_status = ‘publish’ OR wpot_posts.post_status = ‘refunded’ OR wpot_posts.post_status = ‘failed’ OR wpot_posts.post_status = ‘revoked’ OR wpot_posts.post_status = ‘abandoned’ OR wpot_posts.post_status = ‘processing’ OR wpot_posts.post_status = ‘active’ OR wpot_posts.post_status = ‘inactive’ OR wpot_posts.post_status = ‘acf-disabled’ OR wpot_posts.post_status = ‘private’) GROUP BY wpot_posts.ID ORDER BY wpot_posts.post_date DESC LIMIT 0, 15Query failed to find posts matching the criteria
Thanks
So you know some SQL? Then you would realize (as would anyone with an understanding of elementary logic) that
WHERE 1=1 AND ( 0 = 1 )
cannot possibly resolve successfully. WP always includes 1=1 when building queries in the off chance there is no other WHERE criteria to add, then the query will return all posts instead of failing. The impossible 0=1 part is added when WP cannot make sense of one or more query vars that it was fed.There is nothing wrong with your code though. Just to be sure, I used it on my own site, using post type and cat ID values appropriate for my site. As expected, it works just fine. That rules out all but what I changed as suspect. We know “download” is a proper value for post type. Are you absolutely sure 112 is a valid cat value? Supplying an invalid value will lead to 1=0 in SQL. There is another possibility, so 1=0 is not proof you’ve done anything wrong.
Please go to the categories list table and pick Edit for the AllyPally category. On the edit screen, look at the URL in your browser’s address bar. Within the URL is something like
tag_ID=###
. What integer appears in place of the hashes? This is the correct ID for AllyPally, presumably it should be 112. If not, change your code to use the number shown. If it is 112, your code is 100% correct. (WP uses tag_ID for all taxonomy term IDs, not just for tags)If your code is correct, the only other explanation is some other code is corrupting the query somewhere. We can narrow down the cause to a particular module by installing and activating the health-check plugin. In the Dashboard section of the admin menu, open the health check page and go to the troubleshooting tab. Activate troubleshooting mode. Be sure your installation has one of the twenty* themes available. Install one if not. I like to use one back from the latest, twentysixteen for now.
You will be placed in a bone stock copy of your site with no plugins active. Check your code again. If it still fails, the conflict is within your own theme. If it now works, reactivate your plugins one by one using the admin bar troubleshooting item. When your code again fails, the last activated plugin is the cause. Take up the issue with the responsible author.
If your code failed due to your own theme, you can prove it was not your code by temporarily copying your code onto a template of the default theme used by troubleshooting. Activate a default theme using the troubleshooting admin bar item. Go to the themes area to verify which theme is used (usually twentyseventeen). Copy your code to one of its templates. page.php or single.php are good candidates. Be sure to place it inside <?php ?> delimiters. Load a page that uses that template. Observe that your code works now, proving the problem is elsewhere in your theme. Again, take up the issue with the responsible author.
Hi there
I spotted the 0=1 line and made no sense of it (it’s a long time since I’ve used SQL)
The category id is 112 (I’ve tried different id’s with the same result).
Now for health-check – I’m in troubleshooting mode and for some reason it wants to run TwentySeventeen but I’ve no idea how to run my php script as I can’t point a page at the template file the script is on (it’s the only way I know how – I was only on the first lesson of a beginers tutorial!).
I’ve switched on the “use current theme” option and run the script (with no plugins) with the same results.Many thanks
Yeah, it sounds like there’s a problem/conflict within your theme somewhere. To see that your code otherwise works with twentyseventeen, first the theme has to be available on your site. Install it through the themes admin screen like any other theme from the wp.org repository. You then need to temporarily place your code on a theme template somewhere. Make a backup of the chosen template file for restoration later. page.php would be a good choice. Place your code anywhere within a <?php ?> block, but stay out of the Loop. This doesn’t leave you with too many options, any one of them is fine. You’ll need your code to query for a normal ‘post’ post type instead of ‘download’, and naturally use a category ID actually used by posts. The ‘download’ post type will not be valid in troubleshooting mode.
Only place the code that you’ve posted here which we’ve corrected. Including too much other code from your theme could end up transferring the same error. The idea is to test only your code that I’ve already tested and is known to work at least on my site. There is a slight chance the problem is within your core installation and not your theme. This will tell us for sure.
Invoke troubleshooting mode again, then load any page from your site. Your code’s output will appear somewhere on the page. Provided there are indeed posts using the specific category specified in your code, they will be listed. This proves your theme was the source of the trouble.
If by chance you still get the “Query failed to find…” message and you’re sure posts within the specified category exist, then your WP core installation must have become corrupted somehow. To resolve this, perform a manual update, except use a fresh .zip download of the same version you are currently using. The idea is to replace all the core files, not actually update. Older versions are available in the release archive.
Hi there,
I won’t get a chance to do this until Friday, possibly Monday (busy weekend!)Thanks
np, you know where to find me ??
I hope busy means fun. Either way, be safe.
Hi,Back again
I’ve been playing with the Health Check plugin:
I copied the code onto a new template file, copied this file into TwentySeventeen and a EDD theme called Themedd (it took someone weeks to think up that name) with the same results as before.
'category_id' => 'bike rides', 'posts_per_page' => 15
ie cat=> 112 returns “query failed to find”, cat=>’anything’ is ignored and the first 15 products are listed.
The manual update looks a bit scary so I’ll wait until tomorrow
Thanks
PS As for the weekend, here in London there was a art festival called Lumiere which I managed to get involved in somehow (v cold, v wet, great fun but knackered!).Uh, yeah, category_id isn’t a valid argument for WP_Query. You’re limited to the arguments listed in the docs. And passing strings like ‘anything’ is also invalid for ‘cat’, it must be an integer like 112. I like your perseverance in trying anything though ?? I feel like I’m missing something, but cannot imagine what it might be. It’s frustrating sometimes in the limits of what can be done through these forums, but it’s for everyone’s protection.
If you’ve not used FTP before, or perhaps don’t even have a FTP client, it is rather involved, but nothing to fear. Your hosting cPanel or similar account management tool should also have a way to move files. Honestly, a stand alone client is better, but it’s more fiddly to set up. Actually replacing files isn’t all that bad. Just be sure to keep your wp-config.php file and wp-content folder.
It just occurred to me, that because of time differences, my FTP encouragement may be late in coming. If so, I hope it went OK.
Oooo – art festival!! Sounds awesome! Too bad it was wet. It rains a lot in winter here too. Everyone just perseveres, can’t let it stop you or nothing happens. People here think temps around 5C is “cold”. I used to live where getting above 0 was “warm”. I acknowledge that near freezing and wet can be a dangerous combination if one is not prepared for it. But, yeah – ART!!, that makes up for it ??
Hi there,
Sorry, I was too tired last night – I meant ‘cat’ not ‘cat_id’.
As for the Manual Update, I’m struggling so I’ll finish it tomorrow.Thanks
Hi there,
I’ve replaced wp-admin & wp-includes with squeaky clean new folders but now everything’s gone blank, front and back ends.
I changed config.phpdefine('WP_DEBUG', false);
to true and got:Fatal error: Call to undefined function wp_cache_get() in /home/k21ageingdj/public_html/wp-includes/functions.php on line 1364
Fatal error: Call to undefined function wp_cache_close() in /home/k21ageingdj/public_html/wp-includes/load.php on line 681
But there is nothing on line 1364
Help!
Sorry for the slow reply. I’m not abandoning you! I was traveling yesterday.
The manual update you did was incomplete. Did you replace all the wp-* files in public_html? EXCEPT wp-config.php. Also in the same folder replace index.php and xmlrpc.php. There are a couple others but they are not important. Did you delete the contents of the other folders before updating? If not, you’ll need to do these folders again. I’m sorry the process gave you trouble. It’ll go a lot easier this time. You’re an expert now!
Hi,
I made a complete horlicks of it and ended up getting my host to restore Sunday’s copy – so I don’t fancy doing that again!.
(It turns out I have “auto update” active and the system files get updated automatically).
Anyway, I’ve tried a couple of things:
I tried the EDD shortcode[downloads category="112"]
to prove that category 112 exists which worked.
Then I tried a simple loop on normal posts to prove there’s nothing on my pc stopping the loop from working:// The Query query_posts( array ( 'cat => 110', 'posts_per_page' => -1 ) ); // The Loop while ( have_posts() ) : the_post(); ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endwhile; // Reset Query wp_reset_query();
Which worked a treat.
Which seems to prove it’s the combination of the loop and custom post that’s the problem, could it not be a tax-query (although I’ve not got one to work)?$args = array( 'post_type' => 'edd_download', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => 'allypallly', ) ) ); $query = new WP_Query( $args ); if ($query->have_posts()) : while ($query->have_posts()): $query->the_post(); ?> <h2><?php the_title(); ?></h2> <?php endwhile; else : echo 'Query failed to find posts matching the criteria <br>'; endif; wp_reset_postdata();
Have a great weekend.
I’m on the road, so my normal pattern of responses will be erratic for a few days.
OK, so with that simple loop, if you were to add post type download to the argument array, nothing is listed? (Your version had a small syntax error that WP can still work with. It’s best to do it correctly though. It’s related to the ‘cat’ argument. I’ve corrected it in my code below – moved the closing single quote)
query_posts( array ( 'cat' => 110, 'posts_per_page' => -1, 'post_type' => 'download' ) ); // The Loop while ( have_posts() ) : the_post(); ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endwhile;
FYI, query_posts() is not the best choice, but it’ll work for now. We’ve bigger issues.
Do you know if the download post type entries have normal ‘publish’ status applied? I just noticed in the SQL that contained the 1=0 bit, that there were many non-standard statuses in the query, such as ‘refunded’ ‘failed’ ‘revoked’ ‘abandoned’ ‘processing’ ‘active’ ‘inactive’ and ‘acf-disabled’. These may be fine in some situations, but it indicates when the SQL was generated, additional code was altering the query somehow. It would not be the direct cause of the 1=0, but some related code could be altering the query in a way that does cause the 1=0. Or maybe one of these alternative statuses are required in order to find downloads, so when a normal query unaltered by plugins is run, nothing is returned because these alternative statuses are not considered.
I think maybe it would be best to go with a raw SQL query that other plugins would not mess with. See if this code results in a list of download posts:
global $wpdb; $query = $wpdb->get_results( "SELECT $wpdb->posts.post_title FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) WHERE $wpdb->term_relationships.term_taxonomy_id = 112 AND $wpdb->posts.post_type = 'download' AND $wpdb->posts.post_status = 'publish' GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC;" ); foreach ( $query as $post ) { echo "{$post->post_title}<br>\n"; }
Put this on the page with your other code that isn’t returning anything, either before or after. Be sure it’s within <?php ?> tags. This has been tested on my site, only changing the post type and category ID to match my data. While plugins could still mess with this, it’s very unlikely. If it works, we can further refine it to meet your needs. If it fails, there’s only 3 criteria, 2 of which we’re pretty sure of, leaving only the status. We’ll then need to investigate further what the proper status is. One way or another, one of these is going to work!
BTW, I checked out the website for the Lumiere Festival you attended. It looks incredible!! No wonder you were knackered! I hope they can keep on doing it. Maybe I could find my way over sometime ??
- The topic ‘How to get “cat=>” working’ is closed to new replies.