wordpress get the first 4 and then the second 4 posts - php

I am making a wordpress template. I have a news category.
I want to display the first 4 posts in 1 column, and the next 4 in the second column and all the remainder in column3. I know how to create the columns and so forth.
My questions is how do i actually get at the first 4, and the second 4 and then the remaining posts after I have started the query.
query_posts('cat=145');
Thanks

you can use offset in query_post, as query_post uses WP_Query in the backend
// First four posts
query_posts('cat=145&posts_per_page=4');
// Second four posts
query_posts('cat=145&posts_per_page=4&offset=4');
Hope it helps.

Related

How to use where and skip in same condition for Laravel queries?

I have a news website and in a partial i must skip first 5 headline articles and fetch others.
There are 2 parts on my page. One is slider ( for headline posts ) and second is for other posts ( non-headline articles and headline articles after last 5 )
My query is not working. I think i need two conditions on my where query.
skip where is headline and last 5 posts..
This query does not work :
->where('headline', '=', '1')->take(15)->skip(5)->get();
When i use this query , it also skip my non-headline posts in last 5 posts. But i want to show them.
How can i achieve this ?
If I understand correctly, you want to show two sets:
Headlined posts (5x)
All posts, except first 5 headlined posts
so:
$headliners = Post::where('headline', '=', '1')->take(5)->get();
$regulars = Post::whereNotIn('id', $headliners->pluck('id'))->get();

Wordpress Get Multiple Posts from Multiple Categories with one query

Using wordpress for a client (I wouldn't over wise).
On their home page I want to get the latest 4 posts for each of 4 different categories, I then want to get 3 additional posts that are latest from any category and not currently used above, followed by 5 with the greatest number of page views in the last 2 weeks.
All this is easy but to use wordpress get_posts would require 6 different calls to the database, can this be done with one instead?
The Procedure:
Get 4 latest posts from the category NEWS
Get 4 latests posts from the category POLITICS that were not already fetched from the first four (there is a possibility that posts are in both categories)
Get 4 latest posts from category FEATURES that were not already fetched
Get 4 latest posts from category COMMENT ... etc..
Get the 3 latest posts from any category that were not yet fetched
Get 5 posts with the greatest number of page views (stored in meta data under the name meta-page-views) that were not yet fetched
Is this possible without multiple queries.
Thanks,
There is no way to do this in one call with usual wordpress functions. It might be possible with different categories in one query but you can't have different sorting orders in one query.
If you really really have to use one query, you could pull for example the latest 100 posts, store them in an array and then use PHP to sort and filter the result to your categories. However, this won't work well with the page views. Also, a category that hasn't been posted within the last 100 posts wouldn't appear on your page.
I don't see a point in using one query only since it only creates problems. Even the most popular wordpress themes don't do that.
If your client is concerned about server load you should probably talk to them about caching.

Get post position in a category

here is my issue.
I am currently in a post (so in single.php). I got the only category that this post is in by using:
$category = get_the_category()[0];
Now, what I would like to be able to know is the current position of that post in that category and I need to be able to retrieve that position without searching through all pages of the category (for performances reasons).
E.g:
Let`s say my post is the 14th most recent post in its category, I want to get that value (14) so that I can then calculate on which category page it would be (knowing how many posts are displayed by page)
Thanks
If you know the number of posts/page ahead of time, you can just use SQL to do this.
Lets say you want to do 10 posts/page.
SELECT * FROM articles WHERE category_id = :categoryID LIMIT 0, 10;
That will give you the first 10 articles in a given category. If you see the articles for page two, you just modify the LIMIT

Numbering products in admin

I have been asked if I could number products in the admin area of OpenCart 1.5.
The numbering would be like:
1
2
3
4
5
etc and continues on the next page.
I am just unsure how to do the count and pull the numbers through into the view.
I've never used OpenCart 1.5 before, however, with a little PHP you could make it work:
Your result array is probably indexed. Add 1 to the index and you have your row number.
For pagination support you just need to use the following equation: (index + (page_size * (page_number - 1)). Then, you'd just add one for each item.
Depending on how your product table is set up, you might also be able to output the primary key of the table. Quite often, those will start at one and increment by one each time. The problem with this, however is that if you delete product #3 then you'll have a gap in your numbers.
Good luck!

How to display 3 previous posts in article page?

Does anyone know how to display 3 previous posts on article page? Let's say I'm in article post page and would like to display previous 3 posts images (basing on the post I'm currently reading) below it's content, but not next posts images. I was trying to do this with setting up the offset in the db query but with no luck. Can someone please help me with this one? I would greatly appreciate any help or at least pointing me in the right direction.
That shouldn't be too hard, if you use an id field with auto increment values, you could do something like this:
SELECT id, name FROM articles WHERE id < {current_id} ORDER BY id DESC LIMIT 3;
Obviously, replace {current_id} with the id of the article you're currently reading.
After displaying the specific post do a new WP_Query to retrieve the 3 posts previous to the publication date of the displayed post. This documentation page describes how to query for posts with a relative date (e.g. the example Return posts from the last 30 days). Base the query on the publication time of the displayed post.
The example includes a way to supply a WHERE clause to the query, with add_filter(). I'm not sure, but I think you need to call remove_filter after doing the query, or that filter might be applied to other queries also.

Categories