Insert a Variable within a WP_Query (Wordpress) - wordpress

I am trying to get wordpress to pull an ID from a variable to use within a category query but for some reason it isn't working. It's probably something with the syntax could you just give me a helping hand.
Here is what I have...
$catPosts1 = new WP_Query('category=$cat1&offset=5&posts_per_page=3');
Basically what I want it to do is get the category ID from $cat1 (I have tested and it is entering a category id in the variable), offset the number of posts by 5 and display 3 posts linked with that category. At the moment the code is just displaying posts offset by 5.
Any ideas?
Mark

Now you're just sending $cat1 along as a string, the code should look like this.
$catPosts1 = new WP_Query('category='.$cat1.'&offset=5&posts_per_page=3');

If it is showing, literally, "$cat1" in the output, you might need to switch from single quotes to double quotes to get the substitution.
That is to say, do this:
$catPosts1 = new WP_Query("category=$cat1&offset=5&posts_per_page=3");
...if you're trying to get the contents of the variable into the WP_Query call.

use double quotes instead of single.it is simple.

Related

How to autocomplete only city(city-town-village)

Request 'https://places.cit.api.here.com/places/v1/autosuggest?app_code=[app_code]&app_id=[app_id]&at=0,0&q=budapesht' return the list of different places, but I need only places with category city-town-village. How to filter only cities?
If you use this query
https://places.cit.api.here.com/places/v1/autosuggest?at=47.4799441%2C18.9826451&q=town&result_types=category&app_id=[app_id]&app_code=[app_code]
you will only get back results of type "category" that start with "town". That's not what is being asked for.
You can set "result_types=address" so that you don't get back chain, category, or place results, but currently there's no way of filtering such that you only get city-town-village. You will also get administrative areas, streets, etc.
Please try use parameter result_types=category and q="townhalls" or "government-community-facility" in autocomplete suggestions API.
And the category "city-town-village" seems no longer valid.
https://places.cit.api.here.com/places/v1/autosuggest?at=47.4799441%2C18.9826451&q=town&result_types=category&app_id=[app_id]&app_code=[app_code]

Wordpress APIs how to list all posts in category using rest route

I know of wp-json, but I cannot use it. I need to use ?rest_route
While this returns first 10 posts in any category
?rest_route=/wp/v2/posts
I would like to display specific category i.e 60
None of the combinations I tried work
ie. ?rest_route=/wp/v2/posts/?categories=60
Any ideas?
Try with & instead of ? like this - http://www.example.com/?rest_route=/wp/v2/posts/&categories=60.
You can also have multiple categories separated by comma like http://www.example.com/?rest_route=/wp/v2/posts/&categories=60,61.

Wordpress order categories

I have a Wordpress site where a lot of the categories are peoples names. The slug becomes john-smith fred-wilkinson etc by default. So the category list is in alphabetical order by their first name. I would like them to list by last name.
I could get the authors/editors to change the slug to wilkinson-fred etc but I would rather have a solution that they don't have to alter the default slug.
I know there are plug-ins that you can drag and drop to any order you want. Again this relies on the author/editor putting it in the right place.
My slugs currently look like this
john-smith
fred-wilkinson
Alan-t-jones
etc
I have other categories that I don't want to alter. I only want to reorder where the parent category is "artist".
Any ideas? If I put wilkinson-fred in the description field can I sort on that?
Thanks
I assume that you want to change the order of wp list categories function output (maybe make it a little clearer in your future SO questions!). If that's so, I think you have two options here:
1) Hijack the orderby parameter as mentioned in this answer by #Chris_O at Wordpress Stackexchange:
There is an unused column, term_order, in the wp_term_relationships table that you can use to assign a custom order to the terms within your taxonomy. The order is set at 0 by default and it will take a custom query to get the order back and another solution to set the order.
Although as #Kaiser mentions in the comments to the answer, this sollution is a little hacky as it's not meant to be used this way and its implemantation may change in future releases of Wordpress.
2) Store the output of the function in varible (echo => false under $args), manipulate the output and print the resulting array. This is the way I would choose as it makes you more in control of the situation.

Mailchimp RSS loop, get index

In RSSITEMS loop, I would like to find a way to get an index variable (0,1,2,3,4 for 5 items) so I can display items at different places. I couldn't find a way to do that, so I passed a variable in RSS tag, but I can't access it with conditional tags like *|IF:RSSITEM:COMMENTS_URL = 1|* (it simply doesn't work but I can print the variable without problem (1).
Mailchimp support told me it's not possible. Instead, I modified the feed to include 1 item which is HTML with my multiple feeds parsed. I now realize I could have used |FEED:...| merge tag with a standard campaign.

In Wordpress: Group same-day posts on the index page

i'm looking for a way to code the loop in such a way that it places posts that are in the same day within a div. Im doing this so I can put a separator between the different days to show users that they are looking at another day. An example of this in action is in informedlondon.com, where once a days worth of posts ends, there is a little graphic in between to separate the posts. This is what Google's Blogger platform does by default, thats why it was easy for the divider to be put between the different days of posts.
I'm guessing there must be a tutorial about it somewhere but I have checked and can't seem to find one. I have a sneaky suspicion that it's very easy and i'm just being a dunce. Would appreciate some help.
Many thanks in advance.
The simplest method to accomplish things like this is to 'hold' the current date in a variable. Before your loop: var $current_date and then inside your loop, a simple if statement:
if( $current_date != get_the_date('d-m-Y') ){
echo '<h2>Posts for '.get_the_date('d-m-Y').'</h2>';
$current_date = get_the_date('d-m-Y');
}
So in effect, you're checking with each post whether the date of the post is different than the date of the last post rendered (in the 'holding' variable, $current_date). If it is, you print a title with the new date, and set the 'holding' variable to the new date. If not, nothing happens and the post is printed as usual.

Resources